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

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


当前回答

我知道这是一个超级老的帖子,但对我来说,我有一个32位python和64位python安装的问题。一旦我卸载了32位的python,一切都正常工作了。

其他回答

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导入机制确实有效,所以:

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

这是python路径问题。

在我的例子中,我安装了python:

/Library/Frameworks/Python.framework/Versions/2.6/bin/python,

在python2.6中没有site-packages目录。

找到我用pip安装的包(SOAPpy)

/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

并且site-package不在python路径中,我所做的只是将site-packages永久地添加到PYTHONPATH中。

打开终端 输入open .bash_profile 在弹出的文本文件中,在最后添加这行: 出口到PYTHONPATH = $ PYTHONPATH: /系统/图书馆/框架/ Python.framework /版本/ 2.6 / lib / python2.6 /网站/ 保存文件,重新启动终端,就完成了

我也遇到过同样的问题,但上面的答案都没用。我快被逼疯了,直到我注意到那个系统。当我从父项目导入后,路径就不同了。原来我用importlib写了一个小函数来导入一个不在项目层次结构中的文件。坏主意:我忘了我做过这件事。更糟糕的是,导入过程搅乱了系统。路径,然后就这样走了。非常糟糕的主意。

解决方案是停止这种情况,并简单地将我需要导入的文件导入到项目中。另一种方法是将文件放到它自己的项目中,因为它需要不时地重新构建,并且重新构建可能与主项目的重新构建不一致。

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

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

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

这招奏效了。