冻结(.exe)traitsUI程序,实际可行吗?

时间:2015-01-22 01:12:56

标签: python enthought pyinstaller cx-freeze traitsui

我试图用cx_freeze或pyInstaller冻结TraitsUI程序,该程序使用Chaco,Traits,TraitsUI以及较小程度的mayavi(实际上可以取出)。我需要这个在mac,linux,ubuntu上运行,所以我避免使用py2exe。 我故意卸载了pyqt和pyside,以便只有wx后端可用。

使用cx_freeze,我遇到并报告了一个错误,所以pyInstaller似乎让我走得最远。它生成了一个.exe文件,但是当我运行该文件时,我收到警告和错误。为了生成.exe文件,我做了:

pyinstaller --onefile --name=pameapp pamemain.py 
pyinstaller pameapp.exe

运行时,警告为:

Warning: Unable to import the wx backend for pyface due to traceback: Traceback (most recent call last):
      File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 45, in _init_toolkit
      File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 31, in import_toolkit
    ImportError: No module named wx.init

Warning: Unable to import the qt4 backend for pyface due to traceback: Traceback (most recent call last):
      File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 45, in _init_toolkit
      File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 31, in import_toolkit
    ImportError: No module named init

Warning: Unable to import the null backend for pyface due to traceback: Traceback (most recent call last):
      File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 45, in _init_toolkit
      File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 31, in import_toolkit
    ImportError: No module named null.init

另外,我收到以下错误:

Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.api", line 35, in <module>
  File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors", line 22, in <module>
  File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors.api", line 10, in <module>
  File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors.code_editor", line 36, in <module>
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors.code_editor", line 48, in ToolkitEditorFactory
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traits.traits", line 487, in __call__
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traits.traits", line 1191, in Color
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.toolkit_traits", line 7, in ColorTrait
  File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.toolkit", line 137, in toolkit
traits.trait_errors.TraitError: Could not find any UI toolkit called 'null'

pyface可能有很多动态导入,所以这一定是任何试图冻结ETS的人遇到的问题。是否有一个单一的解决办法让这个工作?

考虑到在网络上流传的老东西成功率冻结ETS计划,是否值得进一步投资?对我们来说,让人们避免为这个应用程序配置python将是一个巨大的优势。

感谢。

1 个答案:

答案 0 :(得分:1)

您正在使用的Pyface.toolkit使用动态导入:

def import_toolkit(tk):
    try:
        # Try and import the toolkit's pyface backend init module.
        be = 'pyface.ui.%s.' % tk
        __import__(be + 'init')
    except:
        raise
    return be

所以pyinstaller无法检测到这一点。检查python -v b.py 2>&1 | grep imp具体导入是什么,并使用正确的hiddenimports列表添加挂钩文件(hook-.py)。

但是我在本地进行了一些测试,我无法使用__import__进行测试模式。这很奇怪,因为我做了一些调试,我发现hiddenimport已添加,但仍无法使用。