2017-12-24 7 views
0

なぜ私ははImportError:名前をインポートすることはできません 'BeautifulSoup'

line 1, in <module> 
     from bs4 import BeautifulSoup 
    ImportError: cannot import name '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 

が表示されます。

+0

beautifulsoup4をインストールした後、端末を終了してからもう一度やり直してみましたか? – barryjones

+0

偶然にファイル名を「bs4.py」にしましたか?名前を変更してみてください。 –

+1

@BurhanKhalid Yeap。私はちょうど今、物語の道徳的なものを削除した、pythonファイルの名前をbs4.pyにしないでください。 –

答えて

1

bs4.py


あなたのファイル名を指定しないでくださいPythonは場所のリストそれはdocumentationから、モジュールをチェックしますがあります。

あなたのケースでは

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path . sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

を、それが呼ばれるファイルを見つけましたbs4.pyが実行されていた同じディレクトリにあり、インポートしようとしているものと一致しているため、Pythonは残りのディレクトリの検索を停止しました。

bs4.pyにはオブジェクトBeautifulSoupが含まれていないため、インポートエラーが発生します。

このような名前の衝突は、ファイルの名前を慎重に指定することで回避できます。 (例えば、いくつかのモジュールをモックまたはオーバーライドしようとしているときなど)いくつかの場合には役に立ちます。しかし、ここではそうではありません。

関連する問題