compile python application with setup.py

≯℡__Kan透↙ 提交于 2019-12-12 01:11:45

问题


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.

  1. 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.

  2. 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).

  3. 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.

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

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