简体   繁体   中英

Packaging pygame python scripts for cross-platform

What is the best way to package python scripts using the pygame module as cross platform executables? I need to be able to compile from my Linux machine. I've tried using PyInstaller, however, when I run the compiled executable, it says it can't find the pygame module. When I running it as a python script through the Wing IDE debug it works fine, so I clearly have pygame installed. However, when I try running without the IDE debug it fails to create a windows. Is there something wrong with my script that it can only be run through Wing IDE? How can I package my game so that it work on any computer, Linux or Windows, without even needed python or pygame? Is this even possible?

My source: https://github.com/william1008/TrappedTux

PyInstaller works fine to build a Windows executable that embed both python and pygame.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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