[英]ImportError: cannot import name 'BeautifulSoup'
為什么我得到ImportError: cannot import name 'BeautifulSoup'
line 1, in <module>
from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup'
安裝好了嗎?
pip install --upgrade --force-reinstall beautifulsoup4
Collecting beautifulsoup4
Using cached beautifulsoup4-4.6.0-py3-none-any.whl
Installing collected packages: beautifulsoup4
Found existing installation: beautifulsoup4 4.6.0
Uninstalling beautifulsoup4-4.6.0:
Successfully uninstalled beautifulsoup4-4.6.0
Successfully installed beautifulsoup4-4.6.0
看來是這樣。
不要將文件bs4.py
Python有一個地方列表,它將從文檔中檢查模塊:
導入名為
spam
的模塊時,解釋器首先搜索具有該名稱的內置模塊。 如果找不到,它將在變量sys.path
給出的目錄列表中搜索名為spam.py
的文件。sys.path
從以下位置初始化:
- 包含輸入腳本的目錄(或當前目錄)。
PYTHONPATH
(目錄名稱列表,語法與shell變量PATH
)。- 取決於安裝的默認值。
在您的情況下,它在執行該文件的目錄中找到一個名為bs4.py
的文件,並且由於它與您要導入的文件相匹配bs4.py
停止了搜索其余目錄。
由於您自己的bs4.py
不包含對象BeautifulSoup
,因此出現導入錯誤。
仔細命名文件可以避免這種名稱沖突。 在某些情況下它確實很有用(例如,當您嘗試模擬或覆蓋某些模塊時); 但事實並非如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.