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

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


当前回答

今天,我发现setup.py包也会产生这个问题。

我有一个分类器< 3的设置

setup(
    name='data_reader',
    version='0.1',

    description='data_reader by Mithril ',
    long_description=long_description,

    author='Mithril',

    classifiers=[
        'Development Status :: 1 - Beta',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',

        'Intended Audience :: Developers',
        'Operating System :: OS Independent',

        "License :: GPLv3",

        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development :: Libraries :: Tools',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],


)

我在conda env中使用python 3.7,我发现

pip install .
# or
pip install git+https://github.com/eromoe/data_reader

全部成功,但导入data_reader raise未找到。

经过一些测试,挖掘后只改变分类器

    classifiers=[
        'Development Status :: 1 - Beta',
        "Programming Language :: Python :: 3",
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        "License :: GPLv3",
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development :: Libraries :: Tools',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],

重新安装,导入恢复正常!

其他回答

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

如果上面提到的其他答案对您不起作用,请尝试删除pip缓存并重新安装包。我的机器运行的是Ubuntu14.04,它位于~/.cache/pip下。删除这个文件夹对我有用。

我通过一种组合方法纠正了这个问题。首先,我听从克里斯的建议,打开命令行,输入“pip show packagename” 这提供了已安装包的位置。

接下来,我打开python,输入“import sys”,然后输入“sys”。path'来显示我的python在哪里搜索我导入的任何包。唉,第一步中显示的位置不在列表中。

最后一步,我输入“sys.path.append('package_location_seen_in_step_1')”。您可以选择重复步骤2,以查看位置现在在列表中。

测试步骤,尝试再次导入包…它的工作原理。

缺点呢?它是临时的,每次都需要将其添加到列表中。

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

ModuleNotFoundError: No module named 'somemodule'

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