17
votes

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.

3
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
@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
You are using a 2.6 pip installation. Install pip for 2.7 (separately) and use pip-2.7. - Martijn Pieters♦
@ColonelPanic I edited your answer so I could accept it. Feel free to put it back in your own words. Thanks. - Amanda

3 Answers

37
votes

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)

5
votes

Try this: from bs4 import BeautifulSoup

0
votes

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