用setup.py编译python应用程序 [英] compile python application with setup.py
问题描述
我已经指出了将pygame导出到可执行文件中以进行分发的问题.我仍然有一个问题,当我运行setup.py(我使用python版本3.7.0)并构建应用程序时,该应用程序直接崩溃,我也无法打开UNIX可执行文件. 这就是我到目前为止所做的:
I already pointed at the problem of exporting my pygame into an executable for distribution purpose. I still have the problem that when I run the setup.py (I use python version 3.7.0) and build the app, the app directly crashes and I cannot open the unix executable either. Here is exactly what I did so far:
我的setup.py:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
build_exe_options = {"include_files" : ["pic.png", "sound.wav"]} # there are more files, i.e. all pics and audio files used
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('pythonGame.py', base=base)
]
setup(name='MyGame',
version = '1.0',
description = 'blabla',
options = dict(build_exe = build_exe_options),
executables = executables)
当我运行setup.py通过以下方式创建独立应用程序时:
when I run the setup.py to create stand-alone app via:
python setup.py bdist_mac
我收到(许多)错误消息(参见终端输出的最后3行):
I get (many) error messages (cf. last 3 lines of terminal output):
> error: /Library/Developer/CommandLineTools/usr/bin/install_name_tool:
> input file:
> build/GesaGame-1.0.app/Contents/MacOS/lib/pygame/pygame_icon.icns is
> not a Mach-O file @loader_path/.dylibs/libSDL-1.2.0.dylib error: can't
> copy '@loader_path/.dylibs/libSDL-1.2.0.dylib': doesn't exist or not a
> regular file
或更高
> error: /Library/Developer/CommandLineTools/usr/bin/install_name_tool:
> input file: build/GesaGame-1.0.app/Contents/MacOS/RunningCleats.wav is
> not a Mach-O file
尽管如此,构建文件夹已创建.打开它时,我找到了指定的程序,但是启动后它直接崩溃了. 我在这里做错了什么?我怀疑这与包含的文件有关,但我无法理解.
Nevertheless, the build folder has been created. When opening it I find the specified program, but it directly crashes after starting it. What am I doing wrong here? I suspect it has something to do with the included files, but I am not able to make sense of it.
推荐答案
因为我不知道 macos 环境,并且没有任何要测试的系统,我只能猜测您的安装脚本可能存在的问题.
As I don't know the macos environment and don't have any system to test, I can only guess potential problems with your setup script.
-
cx_Freeze
尚不支持Python 3.7,但存在错误.有一个错误修正,但尚未发布,但是您可以手动应用它,请参见可能是致命的python错误的原因:initfsencoding:无法加载文件系统编解码器?和 Cx_freeze使Python3.7.0崩溃.或者,如果您愿意的话,也可以回滚到Python 3.6.
cx_Freeze
does not yet support Python 3.7, it has a bug. A bugfix exists but has not yet been released, however you can apply it manually, see What could be the reason for fatal python error:initfsencoding:unable to load the file system codec? and Cx_freeze crashing Python3.7.0. Or you can rollback to Python 3.6 if this is an option for you.
动态导入的软件包以及DLL资源(.dll
/.so
/.dylib
)通常不会自动被cx_Freeze
包括在内,您需要让cx_Freeze
使用build_exe
选项packages
和include_files
.否则它们会被包含在错误的位置(请参阅下一点).
Dynamically imported packages as well as DLL resources (.dll
/.so
/.dylib
) often do no get included automatically by cx_Freeze
, you need to tell cx_Freeze
to include them using the build_exe
options packages
and include_files
. Or they get included into the wrong place (see next point).
cx_Freeze
版本5.1.1(当前版本)将软件包冻结到构建目录的lib
子目录中,而主脚本和主脚本目录中的所有相关文件都直接冻结进入构建目录.因此,程序包中任何文件与主脚本或可执行文件的目录之间的相对路径在冻结的应用程序中都会更改(它会获得附加的lib/
).这意味着,如果程序包尝试使用主应用程序目录中的相对路径查找位于程序包目录中的文件,反之亦然,则此机制将在冻结的应用程序中失败.遍历错误消息的堆栈跟踪,对于每个报告丢失的文件,请检查此文件是否在生成目录中,以及冻结的应用程序是否在正确的位置查找它.手动复制丢失"的文件到构建目录或lib
子目录中,直到可以使用为止.一旦确定了文件的正确位置,就可以使用元组(source, destination)
作为include_files
列表中的项目,以允许cx_Freeze
将从source
到特定destination
的文件包括到构建目录中.另请参见中的使用数据文件的常见问题 cx_Freeze
文档.
cx_Freeze
version 5.1.1 (the current version) freezes the packages into a lib
subdirectory of the build directory, whereas the main script and all dependent files in the directory of the main script get frozen directly into the build directory. Thus, the relative path between any file in a package and the directory of the main script or executable changes in the frozen application (it gets an additional lib/
). This means that if a package tries to find a file located in a package directory using a relative path from the directory of the main application or vice versa, this mechanism will fail in the frozen application. Go through the stack trace of the error message and for every file reported missing check whether this file is in the build directory and whether the frozen application looks for it at the right place. Make manual copies of the "missing" files into the build directory or into its lib
subdirectory as necessary until it works. Once you have identified the correct place for the file, you can use a tuple (source, destination)
as item in the include_files
list to let cx_Freeze
include a file from source
to a specific destination
into the build directory. See also the FAQ Using data files in the cx_Freeze
documentation.
作为一般建议,仅使用最小的GUI而不使用其他软件包,将主脚本减少到最小的应用程序,并使它在系统上运行.然后一一重新添加您需要的程序包和依赖项(图标,图片,声音,视频等),并检查未冻结和冻结的应用程序在每一步都能正常工作.
As a general advice, reduce your main script to a minimal application using only a minimal GUI and no further package and make it work on your system. Re-add then the packages and dependencies (icons, pictures, sounds, videos, ...) you need one by one and check that the unfrozen and frozen applications work at each step.
这篇关于用setup.py编译python应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!