2012-11-29 31 views
4

使用py2exe爲我的應用程序準備.exe時出現問題。 此問題的來源是我創建的以下函數,它使用動態定義模塊的 中的類。使用py2exe處理動態導入

def of_import(module, classname, country = None): 
    ''' 
    Returns country specific class found in country module 
    ''' 
    if country is None: 
     country = CONF.get('simulation', 'country') 
    _temp = __import__(country + '.' + module, 
         globals = globals(), 
         locals = locals(), 
         fromlist = [classname], 
         level=-1) 
    return getattr(_temp, classname, None) 

當我嘗試加載使用一些類:

self.InputTable = of_import('model.data', 'InputTable') 

運行.exe文件時,我結束了以下錯誤:

File "core\utils.pyc", line 900, in of_import 
ImportError: No module named france.model.data 

我應該精確的法國。 model.data.py確實存在。

什麼是解決此問題的適當方法?

對於這裏的信息是鏈接到安裝文件:https://github.com/openfisca/openfisca/blob/dev/src/setup_x64.py

+0

兩個問題:是否存在'france.model.data.py'模塊文件,如果存在,那麼在使用'py2exe'編譯它之後,這個代碼只會失敗嗎? – martineau

+0

相關的代碼是你的'setup.py' for py2exe。我們可以看到嗎?它需要包括模塊,因爲它們不會被py2exe自動發現 – jdi

+0

我編輯了問題以回答@martineau問題 – benjello

回答

3

我有一個類似的設置

確保您py2exe的「包裝」部分添加您的動態模塊

setup(windows=[{ 
       "script" : "openFisca.pyw" 
       }], 
     options={"py2exe" : {"includes" : ["sip", "encodings.*", "numpy.*"], 
          "packages": ["france","tunisia"], 
          "dist_dir": dist_dir, 
          "bundle_files":3, 
          "dll_excludes": ["MSVCP90.dll"] 
          }}, 
     data_files=data_files) 
+0

完美答案。謝謝 – benjello