我正在一个没有管理员权限的系统中工作。我的本地环境安装了 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 的吗?
最佳答案
Python 3 无法“拾取”Python 2 包。您还没有安装正确的 bs4。对于 ubuntu,软件包名称为:python3-bs4 。
此外, except
block 中的错误消息具有误导性,因为它指向 python2 bs4,而代码是 python 3。
如果您无法在系统上安装新软件包,请设置 virtual-environment并通过 pip 安装依赖项。
关于python - Beautifulsoup4 选择系统默认的 python2.6 而不是我本地的 python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768958/