Beautifulsoup4 选择系统默认 python2.6 而不是我的本地 python3

Beautifulsoup4 picks up system default python2.6 and not my local python3

我在没有管理员权限的系统中工作。我在本地环境中安装了 python3、pip3 和 bs4。似乎默认系统 python2.6 也安装了 beautifulsoup 软件包。结果,我的 Python 代码尝试选择 bs4 并最终选择了 python2.6 一个。

我的代码是:

try:
    from bs4 import BeautifulSoup
except ImportError as err:
    print("BeautifulSoup is not installed. To install it use apt-get install python-bs4 or visit https://www.crummy.com/software/BeautifulSoup/ for more information. \n OS error: {0}".format(err))
    raise

我正在使用 pip 通过以下命令安装 bs4:

pip3 install beautifulsoup4 
Requirement already satisfied: beautifulsoup4 in /my-local-path/lib/python3.3/site-packages

有没有办法强制它拿起python3一个?

Python3包不了"pick up"python2包。你没有安装正确的 bs4。对于 ubuntu,包名称是:python3-bs4

此外,您在 except 块中的错误消息具有误导性,因为它指向 python2 bs4,而代码是 python 3.

如果您无法在您的系统上安装新包,请设置 virtual-environment 并通过 pip 安装您的依赖项。