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

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


当前回答

我在系统上安装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。(并仔细阅读错误消息)。

其他回答

这工作! !

这通常发生在模块安装到旧版本的python或其他目录时,不用担心,因为解决方案很简单。 - import module from模块所在目录。 你可以先导入python sys模块,然后从模块安装的路径导入

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

import <module_name>

我已经解决了我的问题,相同的库在一个项目(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)的虚拟环境无法识别已经安装和工作的库的问题。

我有类似的问题(在Windows上),根本原因在我的情况下是杀毒软件!它有“自动遏制”功能,用某种虚拟机包装正在运行的进程。 症状是:pip安装someemodule在一个cmd行窗口中正常工作,从另一个进程执行时导入someemodule失败,并出现错误

ModuleNotFoundError: No module named 'somemodule'

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

我在系统上安装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。(并仔细阅读错误消息)。