我使用 requirment.txt 在我的项目目录中安装了 beautifulsoup4:
<块引用>请求==2.26.0 beautifulsoup4==4.9.3 html解析器
pip3 install -r requirement.txt --target='local_lib'
尝试导入和运行时出现此错误:
from local_lib.bs4 import BeautifulSoup
soup = Beautifulsoup(html, 'html.parser')
local_lib.bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html-parser. Do you need to install a parser library?
当我尝试从 bs4 而不是 local_lib.bs4 导入时它有效。 但是,我只想将 local_lib 目录用于模块。
为什么我的 local_lib 出现这个错误,我该如何解决?
答案 0 :(得分:1)
你不能这样做。何时在自己的模块/包之间进行导入 Beautifulsoup4 使用绝对导入。例如:
https://bazaar.launchpad.net/~leonardr/beautifulsoup/bs4/view/604/bs4/builder/__init__.py#L7
from bs4.element import …
即,bs4
必须是顶级包,不能是子包。要使其成为子包,您应该重写整个源代码并使所有导入相对。类似于 this pull-request 到我用 the similar reason 发送的项目 Cheetah3:“这使得可以将 Cheetah 嵌入到其他包中......”