概要
pythonプログラムを実行した際に、下記のようなエラーが出たので、対処法を記載します。
$ python sample.py
Traceback (most recent call last):
File "sample.py", line 6, in <module>
import bs4
ImportError: No module named bs4
下記のように書いていると、エラーが出る可能性があります。
$ vim sample.py
# coding:utf-8
from bs4 import BeautifulSoup ...
原因
ImportError: No module named bs4
書いてある通りですが、beautifulsoup4という名前のモジュールがないよ、といっています。指定したモジュールがインストールされていないことが原因になります。
対応
モジュールのインストール
pipコマンドで、beautifulsoup4をインストールします。
$ pip install beautifulsoup4
Collecting beautifulsoup4
Downloading beautifulsoup4-4.9.1-py2-none-any.whl (111 kB)
|████████████████████████████████| 111 kB 608 kB/s
Collecting soupsieve<2.0
Downloading soupsieve-1.9.6-py2.py3-none-any.whl (33 kB)
Collecting backports.functools-lru-cache; python_version < "3"
Downloading backports.functools_lru_cache-1.6.1-py2.py3-none-any.whl (5.7 kB)
Installing collected packages: backports.functools-lru-cache, soupsieve, beautifulsoup4
Successfully installed backports.functools-lru-cache-1.6.1 beautifulsoup4-4.9.1 soupsieve-1.9.6
確認
python sample.py
インストールできていることが確認できました。