Python:无法在Mac上导入mechanize模块
我用easy_install安装了mechanize模块,但当我尝试导入时,出现了以下错误:
Python 2.6.7 (r267:88850, Nov 21 2011, 14:59:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
为了确认我确实安装了mechanize,我又用easy_install安装了一次,结果确认我已经安装了mechanize:
easy_install mechanize
Searching for mechanize
Best match: mechanize 0.2.5
Processing mechanize-0.2.5-py2.6.egg
mechanize 0.2.5 is already the active version in easy-install.pth
Using /Library/Python/2.6/site-packages/mechanize-0.2.5-py2.6.egg
Processing dependencies for mechanize
Finished processing dependencies for mechanize
我发现不仅是mechanize,我用easy_install安装的大多数外部模块都无法导入。这是因为我安装了macports吗……?
这是我通过 echo $PATH
命令得到的结果:
/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Users/N-H/DevApps/android-sdk-mac_x86/platform-tools:/Users/N-H/DevApps/android-sdk-mac_x86/tools:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/opt/subversion/bin/:/opt/subversion/bin:/usr/bin/java:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/N-H/DevApps/android-sdk-mac_86/tools:/Library/grails-1.3.6/bin:/opt/subversion/bin:/usr/bin/java:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/N-H/DevApps/android-sdk-mac_86/tools:/usr/bin/gcc-4.2
我执行了which python,发现macports似乎是在opt目录下安装了python……(我不太确定)
$which python
/opt/local/bin/python
1 个回答
2
看起来你在自带的 Os X 上安装了 Python 2.6 的 mechanize,但你现在使用的是从 macports 安装的 Python 解释器。
你可以通过以下命令为 macports 上的 Python(比如 Python 2.7)运行 easy_install:
/opt/local/bin/easy_install-2.7