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

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


当前回答

在PyCharm中,我通过更改项目解释器路径解决了这个问题。

文件->设置->项目->项目解释器

File -> Invalidate缓存…之后可能需要。

其他回答

我在系统上安装2.7和3.5时遇到了这个问题,试图用Python-Telegram-Bot测试电报机器人。

在使用pip和pip3安装后,无论是否使用sudo,我都无法让它工作。我总是得到:

Traceback (most recent call last):
  File "telegram.py", line 2, in <module>
    from telegram.ext import Updater
  File "$USER/telegram.py", line 2, in <module>
    from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package

正确地读取错误消息告诉我,python正在当前目录中查找telegram.py。我有一个脚本叫telegram。py当我调用import时,它被python加载了。

总结,确保在尝试导入时,当前工作目录中没有任何package.py。(并仔细阅读错误消息)。

我知道这是一个超级老的帖子,但对我来说,我有一个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 -m pip install -user {package name}

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

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

对我有用的是:

卸载密匙环:

Sudo PIP卸载密匙环

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

sudo -H pip安装密匙环