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

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


当前回答

在重新定位的虚拟环境(venv)中也会出现此问题。

我有一个项目,在根目录中设置了venv。后来我创建了一个新用户,并决定将项目移动到这个用户。我没有只移动源文件并重新安装依赖项,而是将整个项目连同venv文件夹一起移动到新用户。

在此之后,我安装的依赖项被添加到全局site-packages文件夹中,而不是venv中的文件夹中,因此在这个env中运行的代码无法访问这些依赖项。

要解决这个问题,只需删除venv文件夹并重新创建它,如下所示:

$ deactivate
$ rm -rf venv
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt

其他回答

另外,请确保不要将pip3与pip混淆。我发现与pip一起安装的包不能与python3一起工作,反之亦然。

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 .

这解决了所有的问题。

我在尝试使用我通过sudo pip install keyring安装的keyring时遇到了这个问题。正如在其他答案中提到的,在我的情况下,这是一个权限问题。

对我有用的是:

卸载密匙环:

Sudo PIP卸载密匙环

我使用sudo的-H选项并重新安装密匙环:

sudo -H pip安装密匙环

我也遇到了同样的问题,更新setuptools有帮助:

python3 -m pip install --upgrade pip setuptools wheel

之后,重新安装包,它应该可以正常工作:)

问题是,如果setuptools是旧的,那么包就会不正确地构建。

对我有用的是:

python -m pip install -user {package name}

该命令不需要sudo。这是在OSX Mojave上测试的。