【发布时间】:2026-02-19 22:40:01
【问题描述】:
下面是我创建的 game.spec 文件。 运行以下命令时,应用程序被完美创建
pyinstaller --onefile game.spec
运行游戏时,无法找到任何数据文件。在进一步探索中发现它搜索目录 /Users/username 中的所有数据文件,而不是从程序运行的绝对路径。
spec 文件需要改写吗?
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['game.py'],
pathex=['/Users/username/pythonenv/mygame'],
binaries=[],
datas=[('images','images'),
('fonts','fonts'),
('sounds','sounds'),
('pygame','pygame'),
('pygameMenu','pygameMenu'),
('pgzero','pgzero'),
('numpy','numpy'),
('pgzrun.py','.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='game',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='game')
app = BUNDLE(coll,
name='game.app',
icon=None,
bundle_identifier=None)
【问题讨论】: