问题
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:
my 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)
when I run the setup.py to create stand-alone app via:
python setup.py bdist_mac
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
or further above
> 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.
回答1:
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
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.Dynamically imported packages as well as DLL resources (
.dll
/.so
/.dylib
) often do no get included automatically bycx_Freeze
, you need to tellcx_Freeze
to include them using thebuild_exe
optionspackages
andinclude_files
. Or they get included into the wrong place (see next point).cx_Freeze
version 5.1.1 (the current version) freezes the packages into alib
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 additionallib/
). 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 itslib
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 theinclude_files
list to letcx_Freeze
include a file fromsource
to a specificdestination
into the build directory. See also the FAQ Using data files in thecx_Freeze
documentation.
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.
来源:https://stackoverflow.com/questions/54785072/compile-python-application-with-setup-py