Handling dynamic import with py2exe

本秂侑毒 提交于 2019-12-12 11:23:21

问题


I have a problem when preparing an .exe for my app using py2exe. The source of this problem is the following function that I created to use classes from a dynamically defined module.

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)

When I try to load some class using:

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

I end up with the following error when running the .exe:

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

I should precise that the france.model.data.py do exist.

What would be the appropriate way to deal with this issue ?

For information here is the link to the setup file : https://github.com/openfisca/openfisca/blob/dev/src/setup_x64.py


回答1:


I have a similar setup

Make sure you add your dynamic modules in the "packages" section of 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)


来源:https://stackoverflow.com/questions/13629321/handling-dynamic-import-with-py2exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!