Where is BeautifulSoup4 hiding?

2019-01-17 17:07发布

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条回答
放我归山
2楼-- · 2019-01-17 17:14

Try this: from bs4 import BeautifulSoup

查看更多
何必那么认真
3楼-- · 2019-01-17 17:25

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

查看更多
家丑人穷心不美
4楼-- · 2019-01-17 17:28

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)

查看更多
登录 后发表回答