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

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


当前回答

在我的情况下(WIN10主机上的Ubuntu 20.04虚拟机),我有一个混乱的情况,安装了许多版本的Python和不同点的共享库(在文件系统的许多点安装pip)。我指的是3.8.10 Python版本。 经过多次测试,我用谷歌搜索发现了一个建议(但是“对不起,我没有链接”)。下面是我为解决这个问题所做的:

From shell session on Ubuntu 20.04 VM, (inside the Home, in my case /home/hduser), I've started a Jupyter Notebook session with the command "jupyter notebook". Then, when jupyter was running I've opened a .ipynb file to give commands. First : pip list --> give me the list of packages installed, and, sympy wasn't present (although I had installed it with "sudo pip install sympy" command. Last with the command !pip3 install sympy (inside jupyter notebook session) I've solved the problem, here the screen-shot : Now, with !pip list the package "sympy" is present, and working :

其他回答

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

为我工作。

对我来说,它是确保模块的版本与我使用的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

不确定这是否会帮助任何人,但我有一个类似的问题在Mac M1与zsh。原来我在我的.zshrc文件中设置了一个别名命令,与我的python命令(python3)同名。

为了解决这个问题,我只需要取消命令的别名。我跑:

unalias python3

从我的家庭终端和Visual Studio的终端。

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

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

Sudo pip3.7 install <package> . sh

在我的例子中,我运行pip install Django==1.11,它不会从python解释器中导入。

浏览pip的命令,我发现pip show是这样的:

> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...

注意位置显示的是'3.4'。我发现python-命令被链接到python2.7

/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7

就在它旁边,我发现了一个名为python3的链接,所以我使用了它。还可以将链接更改为python3.4。这也能解决问题。