PyQt4的Python导入失败

时间:2014-11-11 02:33:00

标签: python import pyqt4

我正在尝试使用“imp”库导出PyQt4的所有符号。使用内置的“import PyQt4.QtCore”就行了,但是python的代码失败了。 我的测试基于Mac。 在Windows上,似乎如果在QtCore目录下放置一个“ init .py”(空文件正常), “进口QtCore”将取得成功。 但在Mac上,由于某种未知的原因,它失败了。 在Cli:

bash-3.2# python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4.QtCore as f
>>> print f.__file__
/Library/Python/2.7/site-packages/PyQt4/QtCore.so

然而,这种用法失败了。

bash-3.2# cd /Library/Python/2.7/site-packages/PyQt4/
bash-3.2# python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import QtCore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: dynamic module not initialized properly

有人可以解释一下吗?

1 个答案:

答案 0 :(得分:2)

QtCore无法直接导入到python中。 QtCore存在于PyQt4库中。要访问QtCore类,您需要执行以下操作:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from PyQt4 import QtCore
>>> 

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.