简体   繁体   English

PyQt4的Python导入失败

[英]Python import failed for PyQt4

I am trying to use the "imp" library to export all symbols of PyQt4. 我正在尝试使用“ imp”库导出PyQt4的所有符号。 Use the built-in "import PyQt4.QtCore" is OK, but the python's code failed. 使用内置的“ import PyQt4.QtCore”可以,但是python的代码失败。 My Test is basing on Mac. 我的测试基于Mac。 On Windows, it seems that if you put one " init .py" (empty file is OK) under the QtCore directory, "import QtCore" will success. 在Windows上,似乎如果在QtCore目录下放置一个“ init .py”(空文件可以),则“ import QtCore”将成功。 But on Mac, for some unknown reason, it failed. 但是在Mac上,由于某种未知的原因,它失败了。 In the Cli: 在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

However, this usage failed. 但是,此用法失败。

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

Can someone explains it? 有人可以解释吗?

QtCore cannot be directly imported into python. QtCore无法直接导入python。 QtCore exists in the PyQt4 library. QtCore存在于PyQt4库中。 To access QtCore class you need do the following : 要访问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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM