2010-08-19 3 views
2

런타임에 파이썬 모듈을 동적으로로드하는 파이썬 응용 프로그램이 있습니다 (__import__ 사용). 로드 할 모듈은 'plugins'(즉 plugins이라는 하위 폴더 인 __init__.py 등의 패키지)입니다. 모든 것은 파이썬 인터프리터에서 잘 돌아가며 심지어 py2exe를 사용하여 윈도우 바이너리로 컴파일 될 때도 잘 동작합니다.py2app : __import__에 의해로드 될 모듈을 포함시키는 방법?

내가 성공한 OSX 앱을 만들려고했지만 .app을 실행하면 ImportError가 발생합니다 : 'no module named plugins.xxxx'. 나는 .APP 컨텐츠를 탐색 할 때, 나는 폴더 Contents/Resources/lib/python2.5/plugins/의 모든 플러그인 모듈을 볼 수 있기 때문에

나는, 나는 (내가 py2exe에 대한 무엇을 동일 'includes': [...], 'packages':['plugins']) py2app에 대한 올바른 옵션을 준 확신합니다.

그렇다면 앱에서 내 모듈을 찾을 수없는 이유는 무엇입니까 (경로 문제 여야 함)?

편집 :

나는 그것이 작동되도록하는 방법을 발견했지만, 이는 좋은 해결책이 아니다 : 나는 파이썬 (print sys.path 사용) 모듈 검색 경로를 인쇄 할 때 , 나는 것으로 나타났습니다 Contents/Resources/lib/python2.5/plugins/ 폴더가 나열되지 않았습니다. 그러나 Contents/Resources/ 폴더가 있으므로 plugins 폴더를 Contents/Resources 폴더로 이동했습니다. 이제 플러그인이 발견되었습니다. 그러나 나는이 추악한 수동 해킹에 여전히 만족하지 못하고있다.

답변

0

Koo는 다음 코드를 사용합니다. 아마 당신이 py2app에 있다는 것을 감지하기 위해 유사한 것을 할 필요가있을 것이며 그에 따라 임포트를 조정할 것입니다.

http://bazaar.launchpad.net/~openobject-client-kde/openobject-client-kde/5.0/annotate/head%3A/Koo/Common/Plugins.py

def scan(module, directory): 
     pluginImports = __import__(module, globals(), locals()) 
     # Check if it's being run using py2exe or py2app environment 
     frozen = getattr(sys, 'frozen', None) 
     if frozen == 'macosx_app' or hasattr(pluginImports, '__loader__'): 
       # If it's run using py2exe or py2app environment, all files will be in a single 
       # zip file and we can't use listdir() to find all available plugins. 
       zipFiles = pluginImports.__loader__._files 
       moduleDir = os.sep.join(module.split('.')) 
       files = [zipFiles[file][0] for file in zipFiles.keys() if moduleDir in file] 
       files = [file for file in files if '__init__.py' in file] 
       for file in files: 
         d = os.path.dirname(file) 
         if d.endswith(moduleDir): 
           continue 
         newModule = os.path.basename(os.path.dirname(file)) 
         __import__('%s.%s' % (module, newModule), globals(), locals(), [newModule]) 
     else: 
       for i in os.listdir(directory): 
         path = os.path.join(directory, i, '__init__.py') 
         if os.path.isfile(path): 
           __import__('%s.%s' % (module, i), globals(), locals(), [i]) 
관련 문제