【问题标题】:ImportError: No module named zope.interface but modules foundImportError:没有名为 zope.interface 的模块,但找到了模块
【发布时间】:2015-08-25 03:04:19
【问题描述】:

我对 python 很陌生,我正在尝试在使用 virtualenv 时导入一个模块。

当我导入模块时,我得到:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition   4.0.6\helpers\pydev\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named zope.interface

我正在尝试使用以下行导入 zope.interface 模块:

import zope.interface

我通过调用“pip freeze”来仔细检查该模块是否已安装。我还在我的 virtualEnv 站点包目录中找到了模块的位置。 模块的路径是:

virName\Lib\site-packages\zope\interface

在这个目录中,我可以看到 __init__.py 文件。

我的理解是,单独存在 __init__.py 文件会使它成为一个有效的模块?

我通过 sys.path 仔细检查了我的模块的搜索路径,它确实包含目录“virName\Lib\site-packages\”

所以我的问题是这样的。为什么python说找不到模块?

我还需要检查什么?

我也尝试使用以下方法查找模块:

imp.find_module('zope.interface')

但我明白了:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
ImportError: No module named zope.interface

【问题讨论】:

  • 你从哪里运行这个 python 代码?你能提供整个目录结构吗?如果您在与 zope.interface 相同的目录中运行 python 代码,则可以省略“zope”。导入部分
  • zope 中真的存在接口吗?...您能发布更多关于您尝试导入的模块的代码吗?
  • 我从 pycharm 的 python 控制台运行我的代码。我将我的 pycharm 环境设置为使用 virtualEnv。包括我在站点包中签入的那个环境,并在那里看到了 zope.interface...

标签: python python-2.7 python-module


【解决方案1】:

试试

pip install zope.interface

从带有 pip.exe 的目录的命令行提示符,例如

C:\anaconda3\Scripts>pip install zope.interface

我遇到了同样的问题,按照上面的安装后解决了

【讨论】:

  • 同样适用于 Ubuntu 16.04。
【解决方案2】:

我也遇到过同样的问题。稍微解释一下,如果你安装了 PyCharm,它会使用 iPython,而 iPython 也使用zope.interface。我在 Mac 上开发 Pyramid 项目(使用 zope.inerface)并看到这种行为:

系统终端中的 Python

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zope.interface
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named interface
>>> 

系统终端中的 iPython

$ ipython
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import zope.interface

In [2]: 

请注意,它导入时没有错误 - 它使用与第一个示例相同的 Python 解释器!

PyCharm 中的 Python 控制台 根据 PyCharm 文档,这使用了 iPython,我们看到这是从启动输出中尝试的:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -u /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 63304 63305
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
PyDev console: using IPython 3.1.0

import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Users/someone/PycharmProjects/SMS_PUB_API'])

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
In[2]: import zope.interface
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-bc61dfc4e3ea>", line 1, in <module>
    import zope.interface
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: No module named interface
In[3]: 

请注意,如果我使用 PyCharm 的终端功能然后使用系统 Python 和 iPython 控制台,我会得到与上面第 2 个示例相同的结果。

从 PyCharm 运行金字塔 奇怪的是,如果我使用与上述示例相同的项目解释器从 PyCharm(使用 zope.interfaces)中运行我的 Pyramid 项目,它启动得很好

很容易认为zope.interface 模块中带有点命名是一个问题——这通常是用于表示路径的python 保留术语。但是,其他 zope.* 模块导入就好了。我进入 PyCharm 的 Project Interpreter 设置,发现我使用的是 zope.interface 4.1.1,而 4.1.2 已经出局。更新后(在 pycharm 中),它工作正常 - 我现在可以导入 zope.interfaces

注意,这也发生在普通的 Mac 终端中。在写这篇文章之前,我可以用 iPython 导入它。升级 iPython 后,它在终端中工作(我上面的第二个示例)。

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,安装 exe 时导入 zope 失败。因为zope文件夹下没有__init__.py文件。

    easy_install zope.interface-3.8.0.win-amd64-py2.7.exe
    

    像这样用egg安装就可以了

    easy_install zope.interface-3.8.0-py2.7-win-amd64.egg
    

    enter link description here下载彩蛋

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 2021-04-15
      • 2013-03-11
      • 1970-01-01
      • 2018-05-13
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多