在Mac OS上导入时由问题安装的Python模块

时间:2016-09-11 19:03:30

标签: python macos python-2.7

我在beautifulsoup4中安装了python2.7模块,但导入失败。

python模块安装在此目录下。

$ls -l /Library/Python/2.7/site-packages
total 1080
-rw-r--r--   1 root  wheel     119 Aug 22  2015 README
drwxr-xr-x   9 root  wheel     306 Sep  8 22:17 beautifulsoup4-4.5.1.dist-info
drwxr-xr-x  14 root  wheel     476 Sep  8 22:17 bs4
-rw-r--r--   1 root  wheel      30 Sep  8 22:03 easy-install.pth
drwxr-xr-x  34 root  wheel    1156 Sep  8 22:16 pip
drwxr-xr-x  10 root  wheel     340 Sep  8 22:16 pip-8.1.2.dist-info
-rw-r--r--   1 root  wheel  538338 Sep  8 22:03 setuptools-26.1.1-py2.7.egg
-rw-r--r--   1 root  wheel      30 Sep  8 22:03 setuptools.pth
drwxr-xr-x   6 root  wheel     204 Jul 26 21:13 vboxapi
-rw-r--r--   1 root  wheel     241 Jul 26 21:13 vboxapi-1.0-py2.7.egg-info
drwxr-xr-x  32 root  wheel    1088 Sep  8 22:16 wheel
drwxr-xr-x  11 root  wheel     374 Sep  8 22:16 wheel-0.29.0.dist-info
$

输出python shell,导入失败。我正在使用python2.7

$ /usr/bin/python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import beautifulsoup4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named beautifulsoup4
>>> exit()

python sys.path的模块位置为/Library/Python/2.7/site-packages

$ /usr/bin/python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print "\n".join(sys.path)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
/Library/Python/2.7/site-packages/setuptools-26.1.1-py2.7.egg
>>> exit()
$

请说明为什么导入模块失败。

由于

2 个答案:

答案 0 :(得分:0)

该模块名为bs4,而不是beautifulsoup4,请尝试使用:

import bs4  

答案 1 :(得分:0)

试试这个:

 from bs4 import BeautifulSoup