我使用CX_Freeze冻结了我的python程序之一。构建系统在Windows中可以正常工作。我可以创建一个具有可执行文件和必要依赖项的目录,该目录将在任何Windows系统中运行。

当我在Linux中尝试相同的操作时,构建部分

python setup.py


工作良好。但是,当我尝试运行生成的可执行文件时,出现以下错误。

 File "/usr/local/lib/python2.7/dist-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
exec code in m.__dict__
File "test.py", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/guidata/__init__.py", line 540, in <module>
import guidata.config
File "/usr/local/lib/python2.7/dist-packages/guidata/config.py", line 19, in <module>
add_image_module_path("guidata", "images")
File "/usr/local/lib/python2.7/dist-packages/guidata/configtools.py", line 100, in add_image_module_path
add_image_path(get_module_data_path(modname, relpath=relpath), subfolders)
File "/usr/local/lib/python2.7/dist-packages/guidata/configtools.py", line 86, in add_image_path
for fileobj in os.listdir(path):
OSError: [Errno 20] Not a directory: '/home/user/tmp/dist/library.zip/guidata/images'


看来guidata试图在不存在的library.zip/guidata/images目录下查找图像。我确保在Windows和Linux上运行相同版本的guidata,cx_Freeze。感谢您为解决该问题提供的任何帮助。

最小的例子

import guidata
_app = guidata.qapplication() # not required if a QApplication has already been created

import guidata.dataset.datatypes as dt
import guidata.dataset.dataitems as di

class Processing(dt.DataSet):
    """Example"""
    a = di.FloatItem("Parameter #1", default=2.3)
    b = di.IntItem("Parameter #2", min=0, max=10, default=5)
    type = di.ChoiceItem("Processing algorithm",
                     ("type 1", "type 2", "type 3"))

param = Processing()
param.edit()


设定档

import sys
import os

"""Create a stand-alone executable"""

try:
    import guidata
    from guidata.disthelpers import Distribution
except ImportError:
    raise ImportError, "This script requires guidata 1.4+"



def create_executable():
    """Build executable using ``guidata.disthelpers``"""
    dist = Distribution()
    dist.setup(name='Foo', version='0.1',
           description='bar',
           script="test.py", target_name='test.exe')
    dist.add_modules('guidata', 'guiqwt')
    # Building executable
    dist.build('cx_Freeze')

if __name__ == '__main__':
    create_executable()

最佳答案

好。这是我自己的问题的答案。经过大量挖掘之后,我意识到这是guidata和/或python的os.path模块中的错误。这是怎么回事。在guidata模块上的configtools.py文件中,有一个函数get_module_data_path,该函数检查/foo/bar/library.zip/yap之类的路径的父“目录”是否为文件。

    import os.path as osp
...
...
    datapath = get_module_path(modname)
    parentdir = osp.join(datapath, osp.pardir)
    if osp.isfile(parentdir):
        # Parent directory is not a directory but the 'library.zip' file:
        # this is either a py2exe or a cx_Freeze distribution
        datapath = ...


现在测试

osp.isfile("/foo/bar/library.zip/yap/..")


在Windows中返回True,在Linux中返回False。这会破坏代码。尚不清楚Python文档是否说明这是错误还是预期的行为。

目前,我没有解决方案,但有破解方法。我将上面的代码更改为:

    import os.path as osp
...
...
    datapath = get_module_path(modname)
    parentdir = osp.join(datapath, osp.pardir)
    parentdir2 = osp.split(datapath.rstrip(os.path.sep))[0]
    if osp.isfile(parentdir) or osp.isfile(parentdir2):
        # Parent directory is not a directory but the 'library.zip' file:
        # this is either a py2exe or a cx_Freeze distribution
        datapath = ...


一切都很好。

10-06 15:39