PythonAnywhere:import mechanize 给出 ImportError,但是 pip 给出 "Already satisfied"

PythonAnywhere: import mechanize gives ImportError, but pip gives "Already satisfied"

对于新手 Python 的问题,我深表歉意。

我有一个脚本

import mechanize

给出错误:

File "/usr/local/lib/python3.5/dist-packages/mechanize/__init__.py", line 122, in <module> from _mechanize import \
ImportError: No module named '_mechanize'

我认为这意味着模块没有安装或初始化,但是当我打开 BASH 并输入:

$ pip3.5 install mechanize

...这给出了错误

Requirement already satisfied: mechanize in /usr/local/lib/python3.5/dist-packages

我看了 here and here. 后者很接近,但我认为我使用的是正确的 pip 版本。

抱歉,我确定这是糟糕的新手命名法。我怀疑 mechanize 和 _mechanize 之间有区别,但我不知道它是什么。

一般建议:您应该使用 virtual environments,因为这样可以通过在每个虚拟环境中包含包来防止包中出现问题。以这种方式更容易维护 python 模块版本。

在普通 PC 上,您也许可以像这样解决问题(但它不适用于 PythonAnywhere):

cd /usr/local/lib/python3.5/dist-packages/mechanize/
python setup.py install

然后再次尝试导入。

如果失败,请尝试执行以下操作

pip uninstall mechanize
pip install mechanize

顺便说一下,在 PA 上你应该使用:

pip3.5 install --user --upgrade mechanize

更多信息在这里:https://help.pythonanywhere.com/pages/InstallingNewModules