gpt4 book ai didi

PyQt4 的 Python 导入失败

转载 作者:行者123 更新时间:2023-12-01 04:58:21 26 4
gpt4 key购买 nike

我正在尝试使用“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

谁能解释一下吗?

最佳答案

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

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

关于PyQt4 的 Python 导入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26856515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com