简体   繁体   English

打包pygame python脚本以实现跨平台

[英]Packaging pygame python scripts for cross-platform

What is the best way to package python scripts using the pygame module as cross platform executables? 使用pygame模块作为跨平台可执行文件打包python脚本的最佳方法是什么? I need to be able to compile from my Linux machine. 我需要能够从我的Linux机器上进行编译。 I've tried using PyInstaller, however, when I run the compiled executable, it says it can't find the pygame module. 我尝试使用PyInstaller,但是,当我运行编译的可执行文件时,它说找不到pygame模块。 When I running it as a python script through the Wing IDE debug it works fine, so I clearly have pygame installed. 当我通过Wing IDE调试将其作为python脚本运行时,它可以正常运行,因此我显然已经安装了pygame。 However, when I try running without the IDE debug it fails to create a windows. 但是,当我尝试在没有IDE调试的情况下运行时,它无法创建Windows。 Is there something wrong with my script that it can only be run through Wing IDE? 我的脚本只能通过Wing IDE运行吗? How can I package my game so that it work on any computer, Linux or Windows, without even needed python or pygame? 如何打包我的游戏,使其可以在任何计算机,Linux或Windows上运行,甚至不需要python或pygame? Is this even possible? 这有可能吗?

My source: https://github.com/william1008/TrappedTux 我的来源: https : //github.com/william1008/TrappedTux

PyInstaller works fine to build a Windows executable that embed both python and pygame. PyInstaller可以很好地构建同时嵌入python和pygame的Windows可执行文件。

Here is the spec file I'm using: 这是我正在使用的规格文件:

# -*- mode: python -*-
a = Analysis(['MyApplication.py'],
             pathex=['.'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None)

# Remove some weird error message
for d in a.datas:
    if 'pyconfig' in d[0]: 
        a.datas.remove(d)
        break

pyz = PYZ(a.pure)
exe = EXE(pyz,
          Tree('resource', prefix='resource', excludes=[]),
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='MyApplication.exe',
          debug=False,
          strip=None,
          upx=True,
          console=False,
          icon="resource/image/MyApplication.ico")

This will bundle everything (resources as well) into a single executable. 这会将所有内容(以及资源​​)捆绑到一个可执行文件中。 However, you'll need to use sys._MEIPATH to access those resources. 但是,您将需要使用sys._MEIPATH来访问这些资源。 I use this code to detect automatically which path to use: 我使用以下代码自动检测要使用的路径:

def resource_path(relative):
    if hasattr(sys, "_MEIPASS"):
        return os.path.join(sys._MEIPASS, relative)
    return relative

我建议使用cx_Freeze,它相对易于使用,并且支持Windows和OS X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM