importing beautiful soup in python3

nishantsingh picture nishantsingh · Feb 22, 2015 · Viewed 8.5k times · Source

I am trying to import beautifulsoup in python3. I have this line:

from bs4 import BeautifulSoup

It says:

from bs4 import BeautifulSoup
File "/usr/local/lib/python2.7/dist-packages/bs4/__init__.py", line 175
except Exception, e:
                ^
SyntaxError: invalid syntax

I see that it is still trying to use python2.7 package. I have installed beautifulsoup for python3 using pip3 and on checking I found that the package is there in /usr/lib/python3/dist-packages/.
Also on checking sys.path, I found that this path is present. Still I don't get why it is trying to use the older package,

Answer

jfs picture jfs · Feb 22, 2015

If python3 executable has python2.7 in sys.path then it means that the Python 3 installation is broken.

Here's sys.path on my machine:

['',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/home/me/.local/lib/python3.4/site-packages',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages']

Note: no python2.7 directories.

Make sure PYTHONPATH is not set, and there are no stray .pth files in the default list (installation-dependent, above) of Python 3.4 directories that mention python2.7 directories, and PYTHONSTARTUP file or sitecustomize, usercustomize modules do not corrupt sys.path.