在安装mechanize后,我似乎无法导入它。

我已经尝试从pip、easy_install和通过python setup.py从这个repo安装:https://github.com/abielr/mechanize。所有这些都无济于事,因为每次我输入Python交互时,我得到:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
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
>>> 

我之前运行的安装报告已经成功完成,因此我希望导入能够正常工作。是什么导致了这个错误?


当前回答

对我来说,它是确保模块的版本与我使用的Python版本一致。我在一个装有Python 3.6的盒子上构建了这个映像,然后注入到一个恰好安装了3.7的Docker映像中,然后当Python告诉我这个模块没有安装时,我撞了头……

Python 3.6的36m bsonnumpy.cpython-36m-x86_64-linux-gnu.so

bsonnumpy.cpython-37m-x86_64-linux-gnu.so

其他回答

我也遇到了同样的问题,更新setuptools有帮助:

python3 -m pip install --upgrade pip setuptools wheel

之后,重新安装包,它应该可以正常工作:)

问题是,如果setuptools是旧的,那么包就会不正确地构建。

I know, that this is very old post but I didn't find any answer that was useful in my case (I am using arch linux). I had a similar problem installing "nest_asyncio" package which was definitely installed (visible when listing all the installed packages). There is a right way for arch linux users of installing python packages (as it was already explained here by Emanuel Fontelles). In my case the solution was just to uninstall the remaining not-working package (in my case "nest_asyncio") and then installing it again using the following command:

sudo pacman - s python-"nest_asyncio .

这解决了所有的问题。

我有类似的问题(在Windows上),根本原因在我的情况下是杀毒软件!它有“自动遏制”功能,用某种虚拟机包装正在运行的进程。 症状是:pip安装someemodule在一个cmd行窗口中正常工作,从另一个进程执行时导入someemodule失败,并出现错误

ModuleNotFoundError: No module named 'somemodule'

不确定这是否会帮助任何人,但我有一个类似的问题在Mac M1与zsh。原来我在我的.zshrc文件中设置了一个别名命令,与我的python命令(python3)同名。

为了解决这个问题,我只需要取消命令的别名。我跑:

unalias python3

从我的家庭终端和Visual Studio的终端。

Python导入机制确实有效,所以:

你的PYTHONPATH是错误的, 您的库没有安装在您认为的位置 您有另一个具有相同名称的库来掩盖这个库