在安装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
>>> 

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


当前回答

如果上面提到的其他答案对您不起作用,请尝试删除pip缓存并重新安装包。我的机器运行的是Ubuntu14.04,它位于~/.cache/pip下。删除这个文件夹对我有用。

其他回答

也许有点跑题了,但我在导入PyYAML时遇到了问题。指出你需要导入yaml。(我猜是经典的rtfm…)

在重新定位的虚拟环境(venv)中也会出现此问题。

我有一个项目,在根目录中设置了venv。后来我创建了一个新用户,并决定将项目移动到这个用户。我没有只移动源文件并重新安装依赖项,而是将整个项目连同venv文件夹一起移动到新用户。

在此之后,我安装的依赖项被添加到全局site-packages文件夹中,而不是venv中的文件夹中,因此在这个env中运行的代码无法访问这些依赖项。

要解决这个问题,只需删除venv文件夹并重新创建它,如下所示:

$ deactivate
$ rm -rf venv
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt

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'

另外,请确保不要将pip3与pip混淆。我发现与pip一起安装的包不能与python3一起工作,反之亦然。