在安装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,但试图使用Python3.7 -所以我必须使用:

Sudo pip3.7 install <package> . sh

其他回答

我有同样的问题:脚本导入colorama是抛出和ImportError,但sudo pip安装colorama告诉我“包已经安装”。

我的解决方案:运行pip没有sudo: pip安装colorama。然后pip同意需要安装它,安装了它,我的脚本运行了。

我的环境是Ubuntu 14.04 32位;我想我在激活virtualenv之前和之后都看到了这个。

更新:更好的方法是使用python -m pip install <package>。这样做的好处是,由于您正在执行想要在其中安装包的特定python版本,pip将明确地将包安装到“正确”的python中。再次强调,在这种情况下不要使用sudo。然后在正确的位置获得包,但可能具有(不需要的)根权限。

我已经解决了我的问题,相同的库在一个项目(A)中工作良好,但在另一个项目(B)中导入这些相同的库会导致错误。我使用Pycharm作为IDE在Windows操作系统。 所以,在尝试了许多潜在的解决方案,但都未能解决问题后,我做了以下两件事(删除“Venv”文件夹,并重新配置解释器):

在项目(B)中,有一个名为(“venv”)的文件夹,位于外部库/。我删除了那个文件夹。

2-Step 1 (deleting "venv" folder) causes error in Python Interpreter Configuration, and there is a message shown at top of screen saying "Invalid python interpreter selected for the project" and "configure python interpreter", select that link and it opens a new window. There in "Project Interpreter" drop-down list, there is a Red colored line showing previous invalid interpreter. Now, Open this list and select the Python Interpreter(in my case, it is Python 3.7). Press "Apply" and "OK" at the bottom and you are good to go.

注意:这可能是我的项目(B)的虚拟环境无法识别已经安装和工作的库的问题。

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

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

在我的情况下,我还必须为超级用户安装模块。

sudo su
pip install <module>

显然,superuse在某些情况下不能访问普通用户的文件。

对我来说,它是确保模块的版本与我使用的Python版本一致。我在一个装有Python 3.6的盒子上构建了这个映像,然后注入到一个恰好安装了3.7的Docker映像中,然后当Python告诉我这个模块没有安装时,我撞了头……

Python 3.6的36m bsonnumpy.cpython-36m-x86_64-linux-gnu.so

bsonnumpy.cpython-37m-x86_64-linux-gnu.so