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

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


当前回答

就像一个朋友今天为我做的那样,下面是帮助我的方法(我正在使用Windows):

按“设置”->“项目”->“项目解释器”。在右边的窗口中,左边有一行标题为“Project Interpreter”。单击该行,它将打开其他几行。

现在按下“显示全部”行。一扇窗会打开。 在这个窗口中,按下右上角的小“+”符号。

将打开一个新窗口。在左边有4个标签,按最上面的一个,上面写着“Virtualenv环境”。 现在,在右边的窗口中,标记‘Existing Environment’选项。'Interpreter'行将变得清晰可见。按“…”’按钮在行右边。

现在,一个浏览窗口将打开。浏览到安装Python本身的目录。不是有PyCharm的那个。当你到达那里,选择'python.exe'文件并按OK(窗口将消失)。

再次按OK(此窗口也将消失)。

现在在这个窗口中,确保您创建的新行被标记,并再次按OK。

现在,所有安装的包都应该在项目解释器中可见,并由程序读取。

其他回答

我在使用Django时也遇到了类似的问题。在我的例子中,我可以从Django shell中导入模块,但不能从导入模块的.py中导入。 问题是我在安装模块的另一个virtualenv上运行Django服务器(因此,执行.py)。

相反,shell实例在正确的virtualenv中运行。这就是为什么它有效的原因。

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

在PyCharm中,我通过更改项目解释器路径解决了这个问题。

文件->设置->项目->项目解释器

File -> Invalidate缓存…之后可能需要。

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 .

这解决了所有的问题。

对我来说最简单的解决方案,但我在这篇文章中没有提到:

我安装了多个版本的Python,但试图使用Python3.7 -所以我必须使用:

Sudo pip3.7 install <package> . sh