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

其他回答

如果您正在使用虚拟环境,请使用pipenv install <模块名>而不是pip install <模块名>

为我工作。

我已经通过pip安装了colorama,我得到了“ImportError: No module named colorama”

所以我用“find”搜索,找到了绝对路径,并像这样将它添加到脚本中:

import sys
sys.path.append("/usr/local/lib/python3.8/dist-packages/")
import colorama 

这招奏效了。

在我的例子中,我假设安装了一个包,因为它出现在pip freeze的输出中。但是,只是site-packages/*。Dist-info文件夹足以让PIP将其列为已安装,尽管缺少实际的包内容(可能是由于意外删除)。即使所有的路径设置都是正确的,也会发生这种情况,如果您尝试pip install <pkg>,它会说“需求已经满足”。

解决方案是手动删除dist-info文件夹,以便pip意识到包内容丢失。然后,重新安装应该会重新填充意外删除的任何内容

我也遇到了同样的问题,更新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 .

这解决了所有的问题。