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

其他回答

也许有点跑题了,但我在导入PyYAML时遇到了问题。指出你需要导入yaml。(我猜是经典的rtfm…)

在我的情况下(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 :

这是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 /网站/ 保存文件,重新启动终端,就完成了

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或其他目录时,不用担心,因为解决方案很简单。 - import module from模块所在目录。 你可以先导入python sys模块,然后从模块安装的路径导入

import sys
sys.path.append("directory in which module is installed")

import <module_name>