我也遇到过同样的问题。稍微解释一下,如果你安装了 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 后,它在终端中工作(我上面的第二个示例)。
希望这会有所帮助!