Не удалось импортировать Python для PyQt4

Я пытаюсь использовать библиотеку "imp" для экспорта всех символов PyQt4. Использовать встроенный "импорт PyQt4.QtCore" можно, но код питона не удался. Мой тест основан на Mac. В Windows кажется, что если вы поместите один "init.py" (пустой файл в порядке) в каталог QtCore, "import QtCore" будет успешным. Но на Mac по неизвестной причине это не удалось. В Кли:

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 ответ

Решение

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.

Другие вопросы по тегам