PyQt4的Python导入失败

PyQt4的Python导入失败,python,import,pyqt4,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 C

我正在尝试使用“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
bash-3.2#cd/Library/Python/2.7/site-packages/PyQt4/
bash-3.2#python
Python 2.7.5(默认,2014年3月9日,22:15:05)
[GCC 4.2.1达尔文兼容苹果LLVM 5.0(clang-500.0.68)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入QtCore
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
系统错误:动态模块未正确初始化

有人能解释一下吗?

QtCore不能直接导入python。PyQt4库中存在QtCore。要访问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
>>> 

Python需要使用_init__; py.py文件将目录视为包含包;这样做是为了防止具有公共名称(如字符串)的目录无意中隐藏稍后在模块搜索路径上出现的有效模块。在最简单的情况下,uuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,如果在文件夹下添加一个空init.py,“导入QtCore”将成功。是的,确实如此。init.py文件表示该文件夹是库的根目录。需要init.py文件才能使Python将目录视为包含包;这样做是为了防止具有公共名称(如字符串)的目录无意中隐藏稍后在模块搜索路径上出现的有效模块。在最简单的情况下,init.py可以只是一个空文件,但它也可以执行包的初始化代码或设置all变量,稍后将进行描述。