web-dev-qa-db-fra.com

Ubuntu - Comment installer un module Python (BeautifulSoup) sur Python 3.3 au lieu de Python 2.7?

J'ai ce code (comme écrit dans la documentation BS4):

  from bs4 import BeautifulSoup

Lorsque j'exécute le script (avec python3), le message d'erreur suivant s'affiche:

  ImportError: No module named 'bs4'

Donc installé BeatifulSoup par:

  Sudo pip install BeatifulSoup4

Mais lorsque j'essaie de réexécuter le script, la même erreur se produit . En effet, BS4 est installé dans:

  BeautifulSoup4 in /usr/local/lib/python2.7/dist-packages

Mais je veux l’installer et l’utiliser avec python3.3 (car il existe d’autres modules qui ne fonctionnent pas avec python2.7).

J'ai essayé avec:

  virtualenv --python=/usr/bin/python2.7 /usr/bin/python3.3

puis installez à nouveau BS4, mais rien n’est résolu.

Un indice? Merci d'avance

13
dragonmnl

Ubuntu a beautifulsoup emballé. Je l'ai trouvé en exécutant la recherche d'apt-cache

$ apt-cache search beautifulsoup

Je vois qu'il a à la fois une version 2.7 et 3.3 dans les résultats. Vous pouvez obtenir la version 3.3 en installant python3-bs4

$ Sudo apt-get install python3-bs4
29
jrwren

Utilisez pip3

Sudo pip3 install BeautifulSoup4

Si vous ne pouvez pas exécuter pip3, installez-le avec le suivant :

Sudo apt-get install python3-setuptools
Sudo easy_install3 pip 


xxx@Ubuntu14:~/Desktop$ Sudo pip3 install BeautifulSoup4
[Sudo] password for xxx:
Downloading/unpacking BeautifulSoup4
  Downloading beautifulsoup4-4.3.2.tar.gz (143kB): 143kB downloaded
  Running setup.py (path:/tmp/pip_build_root/BeautifulSoup4/setup.py) Egg_info for package BeautifulSoup4

Installing collected packages: BeautifulSoup4
  Running setup.py install for BeautifulSoup4
    Skipping implicit fixer: buffer
    Skipping implicit fixer: idioms
    Skipping implicit fixer: set_literal
    Skipping implicit fixer: ws_comma

Successfully installed BeautifulSoup4
Cleaning up...
xxx@Ubuntu14:~/Desktop$ python3
Python 3.4.2 (default, Oct  8 2014, 13:08:17)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> 
11
blfuentes

Une seule commande a fait l'affaire pour moi:

Essayer:

Sudo apt-get install python3-bs4

puis importez-le en tant que:

from bs4 import BeautifulSoup    
5
Jimmy

J'ai souvent référencé le lien de documentation: https://docs.python.org/3/installing/

Quelques exemples:

 python2   -m pip install SomePackage  # default Python 2 
 python2.7 -m pip install SomePackage  # specifically Python 2.7 
 python3   -m pip install SomePackage  # default Python 3 
 python3.4 -m pip install SomePackage  # specifically Python 3.4
0
kyb