Ubuntu - 如何在 Python 3.3 而不是 Python 2.7 上安装 Python 模块 (BeautifulSoup)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26511791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:34:56  来源:igfitidea点击:

Ubuntu - How to install a Python module (BeautifulSoup) on Python 3.3 instead of Python 2.7?

pythonpython-2.7ubuntubeautifulsouppython-3.3

提问by dragonmnl

I have this code (as written in BS4 documentaion):

我有这个代码(如 BS4 文档中所写):

  from bs4 import BeautifulSoup

When I run the script (using python3) I get the error:

当我运行脚本(使用 python3)时,出现错误:

  ImportError: No module named 'bs4'

So installed BeatifulSoup by:

因此,通过以下方式安装了 BeatifulSoup:

  sudo pip install BeatifulSoup4

But when I try to run the script again I get the same error. Indeed BS4 is installed in:

但是当我再次尝试运行脚本时,我得到了同样的错误。事实上 BS4 安装在:

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

But I want to install and use it with python3.3 (as there are other module which are not working with python2.7).

但我想安装并与 python3.3 一起使用它(因为还有其他模块不适用于 python2.7)。

I tried with:

我试过:

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

and then install BS4 again, but nothing solved.

然后再次安装BS4,但没有解决。

Any clue? Thanks in advance

有什么线索吗?提前致谢

采纳答案by jrwren

Ubuntu has beautifulsoup packaged. I found it by running apt-cache search

Ubuntu 已经打包了 beautifulsoup。我通过运行 apt-cache search 找到了它

$ apt-cache search beautifulsoup

I see it has both a 2.7 and 3.3 version in the results. You can get the 3.3 version by installing python3-bs4

我看到结果中有 2.7 和 3.3 版本。安装python3-bs4即可获得3.3版本

$ sudo apt-get install python3-bs4

回答by blfuentes

Use pip3

使用 pip3

sudo pip3 install BeautifulSoup4

If you cannot run pip3 install it with the following:

如果您无法运行 pip3,请使用以下命令安装它:

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
>>> 

回答by Jimmy

A single command did the trick for me:

一个命令对我有用:

Try:

尝试:

sudo apt-get install python3-bs4

and then import it as:

然后将其导入为:

from bs4 import BeautifulSoup    

回答by kyb

I have often referenced the documentation link: https://docs.python.org/3/installing/

我经常引用文档链接:https: //docs.python.org/3/installing/

Some examples:

一些例子:

 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

回答by Daniel de Araújo

I had the same problem, but when I tried to "sudo apt-get install'python3-bs4" it said it was already installed. I ran my program with "python3 program.py" and it worked just fine.

我遇到了同样的问题,但是当我尝试“sudo apt-get install'python3-bs4”时,它说它已经安装了。我用“python3 program.py”运行了我的程序,它运行得很好。