Kai
Kai

Reputation: 55

no module named bs4 whenever I try to import

I am attempting to create a web scraping program but whenever I write: from bs4 import beautifulsoup, I always get the error:

no module named bs4

I installed bs4 by: pip install beautifulsoup4 and pip install bs4 but nothing is working.

Upvotes: 1

Views: 2306

Answers (3)

Laurence
Laurence

Reputation: 41

Maybe the problem is that your program's project uses virtual environment (venv) without bs4. If it is so - install bs4 directly into your venv on your own:

  1. Open cmd
  2. Type cd path\to\your\project
  3. Find your virtual environment folder ("venv"/"virtualenv"/etc.)
  4. Find "activate" in your venv (for "venv" type in cmd venv\Scripts\activate)
  5. Try to install bs4 one more time.

Note: some IDEs (like PyCharm) have easier ways for it (like 'settings' button or built-in console with activated venv in it).

In your case (for PyCharm):

Variant 1

At the bottom there will be a panel with different consoles, etc: TODO, Problems, Terminal, Python Packages, Python Console,... Open Terminal. it has to have a row like:

(venv) C:\path\to\your\PyCharmProjects\ProjectName>

Use this console to check if bs4 is installed for your project (you can just try to install one more time with pip install bs4)


Variant 2

Press Ctrl+Alt+S -> Project -> Python Interpreter

Check if bs4 is installed for your projects in the appeared packages list. If not: press "+"(button above the list), type in 'beautifulsoup' or 'bs4', choose an appropriate package and click "Install Package"

Upvotes: 1

Sergey Semenov
Sergey Semenov

Reputation: 11

It works

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
from urllib.request import urlopen

data = urlopen('https://my_site/').read()
read_data = BeautifulSoup(data)

Upvotes: 1

Maximilian Freitag
Maximilian Freitag

Reputation: 1049

Maybe check out if your machine even installed it with:

import bs4 

bs4.__version__

Then run:

import bs4 as bs

If it's still not working look at pip itself and re-install it:

pip --version

sudo pip uninstall pip

sudo easy_install pip

Upvotes: 0

Related Questions