在安装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 3.8.0上,但是VS Code使用旧版本(3.4)运行我的脚本。

终端修复:

py . py

确保在正确的Python版本上安装包

其他回答

就我而言,这是许可问题。软件包以某种方式安装,只有根读写权限,其他用户不能读写它!

也有这个问题。这个包安装在Python 3.8.0上,但是VS Code使用旧版本(3.4)运行我的脚本。

终端修复:

py . py

确保在正确的Python版本上安装包

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

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

Sudo pip3.7 install <package> . sh

我一直在用脑袋撞显示器,直到一个年轻的实习生告诉我,秘诀是在模块目录中“python setup.py install”。

出于某种原因,从那里运行设置可以让它正常工作。

需要明确的是,如果你的模块名是"foo":

[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root  118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root   93 Jun 21 15:23 foo
-rw-r--r-- 1 root root  416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->

如果您试图从任何其他目录通过调用其路径来运行setup.py,那么您最终会得到一个borked安装。

不工作:

python /root/foo/setup.py install

做的工作:

cd /root/foo
python setup.py install

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 .

这解决了所有的问题。