Pyinstaller 无法访问数据文件夹 [英] Pyinstaller Unable to access Data Folder

查看:48
本文介绍了Pyinstaller 无法访问数据文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我创建的 game.spec 文件.运行以下命令时,应用程序将完美创建

Below is the game.spec file that I have created. When running the below command, the app gets created perfectly

pyinstaller --onefile game.spec

运行游戏时,无法定位任何数据文件.进一步探索发现它搜索目录/Users/username 中的所有数据文件,而不是从程序运行的绝对路径中搜索.

When operating the game, it is unable to locate any of the datafiles. On further exploration found that it searches for all the datafiles in the directory /Users/username and not from the absolute path the program runs from.

spec 文件需要写不同的吗?

Does the spec file need to be written differently?

# -*- 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)

推荐答案

当 pyInstaller(或 cx_Freeze、py2exe 等)生成可执行文件时,所有程序文件,以及 PyGame、Python 和其他一堆东西都是压缩了.

When pyInstaller (or cx_Freeze, py2exe, etc.) make an executable, all the program files, along with PyGame, Python and a bunch of other stuff is all compressed up.

当它运行时,首先发生的事情是解压资料档案.在某处打开包装.然后你的可执行文件被启动,但不是从解包的位置启动.

When it's run, the first thing to happen is that archive of stuff is unpacked. Unpacked somewhere. Then your executable is started, but not from the location of the unpack.

要解决此问题,您的脚本必须确定它运行的位置 - 即脚本的完整路径.然后使用此位置查找程序的所有额外文件.

To fix this, your script has to determine the location it is running from - that is the full path to the script. Then use this location to find all the program's extra files.

import sys
import os.path

if getattr(sys, 'frozen', False):
    EXE_LOCATION = os.path.dirname( sys.executable ) # cx_Freeze frozen
else:
    EXE_LOCATION = os.path.dirname( os.path.realpath( __file__ ) ) # Other packers

然后在加载文件时,用os.path.join确定完整路径:

Then when loading a file, determine the full path with os.path.join:

my_image_filename = os.path.join( EXE_LOCATION, "images", "xenomorph.png" )
image = pygame.image.load( my_image_filename ).convert_alpha()

my_sound_filename = os.path.join( EXE_LOCATION, "sounds", "meep-meep.ogg" )
meep_sound = pygame.mixer.Sound( my_sound_filename )

可能可以使用 os.chdir( EXE_LOCATION ) 设置一次目录,从而减少更改,但我认为注意路径是最好的方法.

It may be possible to use os.chdir( EXE_LOCATION ) to set the directory once, and thus make less changes, but I think being careful about paths is the best approach.

这篇关于Pyinstaller 无法访问数据文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆