2014-11-11 4 views
1

"imp"라이브러리를 사용하여 PyQt4의 모든 심볼을 내보내려고합니다. 내장 된 "import PyQt4.QtCore"를 사용하는 것은 괜찮지 만, 파이썬의 코드는 실패합니다. 내 테스트는 Mac을 기반으로합니다. Windows에서 QtCore 디렉토리 아래에 "init .py"(빈 파일은 OK)를 넣으면 "import QtCore"가 성공할 것으로 보입니다. 그러나 Mac에서는 일부 알 수없는 이유로 실패했습니다. Cli :PyQt4에 대한 Python 가져 오기가 실패했습니다

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 

누군가 설명 할 수 있습니까?

답변

2

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.

+0

당신이 아니라, 창에 "수입 QtCore가"NO "__init__.py"가없는 것입니다 실패, 창에 가져 오기를 실행 보인다 QtCore 폴더 아래에 빈 __init__.py를 추가하면 "import QtCore"가 성공합니다. – python

+0

네, 맞습니다. init.py 파일은 폴더가 라이브러리의 루트임을 나타냅니다. __init__.py 파일은 파이썬이 디렉토리를 패키지를 포함하도록 처리하기 위해 필요합니다. string과 같은 공통 이름을 가진 디렉토리가 모듈 검색 경로에서 나중에 발생하는 유효한 모듈을 의도하지 않게 숨기는 것을 막기 위해이 작업이 수행됩니다. 가장 간단한 경우 __init__.py는 빈 파일 일 수 있지만 패키지의 초기화 코드를 실행하거나 나중에 설명 할 __all__ 변수를 설정할 수도 있습니다. – TheCreator232

관련 문제