Cannot import Beautiful Soup

僤鯓⒐⒋嵵緔 提交于 2019-12-22 03:33:08

问题


I am trying to use BeautifulSoup, and despite using the import statement:

from bs4 import BeautifulSoup

I am getting the error: ImportError: cannot import name BeautifulSoup

import bs4 does not give any errors.

I have also tried import bs4.BeautifulSoup and just importing bs4 and creating a BeautifulSoup object with: bs4.BeautifulSoup()

Any guidance would be appreciated.


回答1:


The issue was I named the file HTMLParser.py , and that name is already used somewhere in the bs4 module.

Thanks to everyone that helped!




回答2:


I solved it by installing beautifulsoup4, the "4" is essential.

pip install beautifulsoup4



回答3:


I experienced a variation of this problem and am posting for others' benefit.

I named my Python example script bs4.py

Inside this script, whenever trying to import bs4 using the command:

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.

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




回答4:


Copy bs4 and beautifulsoup4-4.6.0.dist-info from C:\python\Lib\site-packages to 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!




回答5:


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.




回答6:


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)

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.




回答7:


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



来源:https://stackoverflow.com/questions/29907405/cannot-import-beautiful-soup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!