Python:无法在 Mac 上导入 Mechanize 模块

标签 python mechanize macports easy-install

我使用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 得到的
回声 $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 看起来像 mac ports 在 opt 目录下安装 python ......(不太确定)
$which python
/opt/local/bin/python

最佳答案

看起来您已经将 Mechanize 安装到随 Os X 提供的 python 2.6,但您正在运行从 macports 安装的 python 解释器。

您可以使用(对于python 2.7)从macports运行easy_install for python:

/opt/local/bin/easy_install-2.7

关于Python:无法在 Mac 上导入 Mechanize 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8378060/

相关文章:

python - 在 python 中计算大型 numpy 数组的速度和加速度

python - 尝试使用 python 和 mechanize 解析保管箱事件页面时出现 403 错误

python - easy_install 和 macports 未安装到 Mac OS X 10.8 上正确的 Python 版本

macos - Macports OSX 10.9.2 : is -m32 compile possible?

python - Macports 只卸载一个 Python 版本

python - 从其他字段计算的 Django 模型字段

python - Python 中针对简单数组查找的小循环的性能问题

python - 生成 python 函数来包装 REST API

python - WIPO 搜索缺少表格数据 POST 消息

python - 使用 mechanize 访问经过 HTTP Basic 身份验证的网页