本文介绍了PyQt4的Python导入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用"imp"库导出PyQt4的所有符号.使用内置的"import PyQt4.QtCore"可以,但是python的代码失败.我的测试基于Mac.在Windows上,如果您在QtCore目录下放置一个" init .py"(空文件就可以了),导入QtCore"将成功.但是在Mac上,由于某种未知的原因,它失败了.在Cli中:

I am trying to use the "imp" library to export all symbols of PyQt4. Use the built-in "import PyQt4.QtCore" is OK, but the python's code failed.My Test is basing on Mac.On Windows, it seems that if you put one "init.py" (empty file is OK) under the QtCore directory,"import QtCore" will success.But on Mac, for some unknown reason, it failed.In the 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

有人可以解释吗?

推荐答案

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

QtCore cannot be directly imported into python. QtCore exists in the PyQt4 library. To access QtCore class you need do the following :

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
>>>

__ init__.py文件是使Python将目录视为包含包的必需文件;这样做是为了防止具有通用名称(例如字符串)的目录无意间隐藏了稍后在模块搜索路径中出现的有效模块.在最简单的情况下,__init__.py可以只是一个空文件,但它也可以为该程序包执行初始化代码或设置__all__变量,如后所述.

这篇关于PyQt4的Python导入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 08:45