Python 不能导入美汤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29907405/
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
Cannot import Beautiful Soup
提问by jhell
I am trying to use BeautifulSoup, and despite using the import statement:
我正在尝试使用 BeautifulSoup,尽管使用了 import 语句:
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
I am getting the error: ImportError: cannot import name BeautifulSoup
我收到错误: ImportError: cannot import name BeautifulSoup
import bs4
does not give any errors.
import bs4
不会给出任何错误。
I have also tried import bs4.BeautifulSoup
and just importing bs4 and creating a BeautifulSoup object with: bs4.BeautifulSoup()
我也试过import bs4.BeautifulSoup
只导入 bs4 并创建一个 BeautifulSoup 对象:bs4.BeautifulSoup()
Any guidance would be appreciated.
任何指导将不胜感激。
采纳答案by jhell
The issue was I named the file HTMLParser.py
, and that name is already used somewhere in the bs4 module.
问题是我命名了 file HTMLParser.py
,并且该名称已在 bs4 模块中的某处使用。
Thanks to everyone that helped!
感谢所有帮助过的人!
回答by mathlogic
I solved it by installing beautifulsoup4, the "4" is essential.
我通过安装beautifulsoup4解决了它,“4”是必不可少的。
pip install beautifulsoup4
回答by Mickey Mouse
I was also facing this type error in the beginning even after install all the modules which were required including pip
install bs4
(if you have installed this then no need to install beautifusoup4 | BeautifulSoup4
through pip
or anywhere else it comes with bs4
itself)
即使在安装了包括pip
install在内的所有必需的模块之后,我一开始也bs4
遇到了这种类型的错误(如果你已经安装了这个,那么就不需要beautifusoup4 | BeautifulSoup4
通过pip
它或它bs4
本身附带的任何其他地方进行安装)
Solution : Just go to your python file where it is installed C:\python\Lib\site-packages
and then copy bs4
and beautifulsoup4-4.6.0.dist-info
folders and paste it to your project folder where you have saved your working project.
解决办法:只要到你的Python文件安装它 C:\python\Lib\site-packages
,然后复制bs4
和beautifulsoup4-4.6.0.dist-info
文件夹并将其粘贴到您保存您的工作项目的项目文件夹。
回答by Mark
I experienced a variation of this problem and am posting for others' benefit.
我遇到了这个问题的一个变种,并且为了他人的利益而发帖。
I named my Python example script bs4.py
我将我的 Python 示例脚本命名为 bs4.py
Inside this script, whenever trying to import bs4 using the command:
在此脚本中,每当尝试使用以下命令导入 bs4 时:
from bs4 import BeautifulSoup
, an ImportError
was thrown, but confusingly (for me) the import worked perfectly from an interactive shell within the same venv environment.
from bs4 import BeautifulSoup
, anImportError
被抛出,但令人困惑的是(对我而言)导入在同一 venv 环境中的交互式 shell 中完美运行。
After renaming the Python script, imports work as expected. The error was caused as Python tries to import itself from the local directory rather than using the system copy of bs4
重命名 Python 脚本后,导入按预期工作。该错误是由于 Python 尝试从本地目录导入自身而不是使用 bs4 的系统副本而引起的
回答by Sadman Amin
Copy bs4and beautifulsoup4-4.6.0.dist-infofrom C:\python\Lib\site-packagesto your local project directory. It worked for me. Here, python actually looks for the library in local directory rather than the place where the library was installed!
将bs4和beautifulsoup4-4.6.0.dist-info从C:\python\Lib\site-packages复制到本地项目目录。它对我有用。在这里,python实际上是在本地目录而不是安装库的地方查找库!
回答by thevisionarist
One of the possible reason: If you have more than one python versions installed and let's say you installed beautifulsoup4 using pip3, it will only be available for import when you run it in python3 shell.
可能的原因之一:如果您安装了多个python版本,并且假设您使用pip3安装了beautifulsoup4,则只有在python3 shell中运行它时才能导入它。
回答by avinash kumar azad
The best way to resolve is, while creating your interpreter select your global python path on your system(/usr/local/bin/python3.7). Make sure that in pycharm shell, python --version appears as 3.7. It shouldn't show 2.7
解决的最佳方法是,在创建解释器时选择系统上的全局 python 路径(/usr/local/bin/python3.7)。确保在 pycharm shell 中,python --version 显示为 3.7。它不应该显示 2.7
回答by psrpsrpsr
Make sure the directory from which you are running your script does not contain a filename called bs4.py.
确保您运行脚本的目录不包含名为 bs4.py 的文件名。
回答by githeko
I found out after numerous attempts to solve the ImportError: cannot import name 'BeautifulSoup4'
that the package is actually called BeautifulSoup
so the import should be:
经过多次尝试解决该ImportError: cannot import name 'BeautifulSoup4'
包实际上被调用后,我发现BeautifulSoup
导入应该是:
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
回答by Abhishek Tripathi
The bs4and beautifulsoup4folders might bee in the site-packages folder. So copy BeautifulSoup4 folder in bs4 and then try the below code. It worked for me.
该BS4和beautifulsoup4文件夹可能会在蜂站点包文件夹。所以在 bs4 中复制 BeautifulSoup4 文件夹,然后尝试下面的代码。它对我有用。
from bs4 import BeautifulSoup
Since you were importing BeautifulSoup from bs4 and in bs4 there was no BeautifulSoup folder. That is why it was showing ImportError: cannot import name BeautifulSoup
.
由于您是从 bs4 导入 BeautifulSoup 并且在 bs4 中没有 BeautifulSoup 文件夹。这就是它显示的原因ImportError: cannot import name BeautifulSoup
。