19

I did sudo pip install BeautifulSoup4 and got an awfully optimistic response:

Downloading/unpacking beautifulsoup4
  Running setup.py egg_info for package beautifulsoup4
Installing collected packages: beautifulsoup4
  Running setup.py install for beautifulsoup4
Successfully installed beautifulsoup4
Cleaning up..

but when I try to use import BeautifulSoup4 or from BeautifulSoup4 import BeautifulSoup4 in a script, python says there's no module by that name.

> import BeautifulSoup
ImportError: No module named BeautifulSoup

Update: pip tells me beautifulsoup4 in /usr/local/lib/python2.6/dist-packages but I'm running 2.7.2+ (and print sys.path sees 2.7 paths) ... so now I need to figure out why pip is putting things in the wrong place.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
Amanda
  • 12,099
  • 17
  • 63
  • 91
  • 2
    Run `pip install beautifulsoup4` again to find out where the package has been installed and then in your Python run `import sys` ands `print sys.path` to see where Python looks for modules – Colonel Panic Oct 15 '12 at 19:56
  • @ColonelPanic re-running pip tells me `beautifulsoup4 in /usr/local/lib/python2.6/dist-packages` but `print sys.path` gives all the paths as `python2.7` ... revising my question. – Amanda Oct 15 '12 at 21:09
  • You are using a 2.6 pip installation. Install pip for 2.7 (separately) and use `pip-2.7`. – Martijn Pieters Oct 15 '12 at 21:20
  • @ColonelPanic I edited your answer so I could accept it. Feel free to put it back in your own words. Thanks. – Amanda Oct 15 '12 at 21:50
  • Related: http://stackoverflow.com/questions/5538329/pip-installing-into-an-older-python-version – Amanda Oct 15 '12 at 21:51
  • Does this answer your question? [ImportError: No module named BeautifulSoup](https://stackoverflow.com/questions/5663980/importerror-no-module-named-beautifulsoup) – AMC Feb 08 '20 at 01:41

4 Answers4

37

Try import bs4. It's unfortunate there's no correspondence between PyPI package name and import name. After that the class names are the same as before eg. soup = bs4.BeautifulSoup(doc) will work.

If that still doesn't work, try pip install again and note the path to the package install. Then in your python console run import sys and print sys.path to make sure that the path is there.

You might need to explicitly specify pip-2.7 or switch to easy_install (or easy_install-2.7)

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • 1
    That doesn't work either, sadly: `ImportError: No module named bs4` – Amanda Oct 15 '12 at 19:40
  • 3
    @Amanda: Then something else is wrong; the answer is certainly correct, see http://www.crummy.com/software/BeautifulSoup/bs4/doc/ – Martijn Pieters Oct 15 '12 at 19:41
  • @MartijnPieters Not sure how the BeautifulSoup docs are going to help me troubleshoot `pip`. – Amanda Oct 15 '12 at 21:17
  • 1
    Manually maintained mapping between PyPI package names and import names: https://github.com/bndr/pipreqs/blob/master/pipreqs/mapping – Pat Myron Nov 03 '19 at 19:20
7

Try this: from bs4 import BeautifulSoup

Galal Ouda
  • 81
  • 1
  • 9
1

I had this issue while using VS Code and the Pylance extension. I was able to resolve it by finding the location of my python packages (in my case it was "c:\python39\lib\site-packages"), and adding that to the external resolution paths in Pylance's settings. Pylance was then able to locate the import. I used below code segment to import BeautifulSoup from bs4.

from bs4 import BeautifulSoup
Chandra Shekhar
  • 598
  • 1
  • 7
  • 25
benbilhorn
  • 101
  • 1
  • 4
0

After trying the easy_install and pip if things dont work then download the tz package from the package website untar it in a folder. Now open cmd window and go to the directory where you unzip the tz and run the command 'python setup.py install' IT should work