简体   繁体   中英

importing beautiful soup in python3

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,

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 .

Perhaps using virtualenv can temporarily solve your problem. Or maybe try pip uninstall beautifulsoup4 That way you're only left with the version pip3 installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM