Rod*_*nto 6 pygame pyinstaller python-3.x
我一直试图从我的.py游戏制作一个.exe,这真的令人沮丧.
我使用的是Python 3.5.2,Pygame 1.9.2和Pyinstaller 3.2.
这个游戏完全像.py一样运行,但是在我输入之后pyinstaller --debug game.py,.exe被构建并运行它,我得到以下内容:
这些是game.py中可能与错误相关的代码行:
from os import path
img_dir = path.join(path.dirname(__file__), 'sprites')
title_screen = pygame.image.load(path.join(img_dir, 'title_screen.png'))
Run Code Online (Sandbox Code Playgroud)
我认为它必须与pyinstaler无法获取我的sprites文件夹有关,因为当我尝试运行时pyinstaller --icon=/sprites/icon.ico game.py,我得到这个:
但是,如果我使用pyinstaller --icon=icon.ico game.py它加载图标就好了.
这是我的spec文件:
# -*- mode: python -*-
block_cipher = None
added_files = [
( '/sprites', 'sprites' ),
( '/music', 'music' ),
( 'Heavitas.ttf', '.'),
( 'Roboto-Light.ttf', '.'),
( 'high scores.csv', '.')
]
a = Analysis(['spec_file.py'],
pathex=['C:\\Users\\rodri\\Documents\\Code\\The Color That Fell From The Sky'],
binaries=None,
datas=added_files,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='spec_file',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='spec_file')
Run Code Online (Sandbox Code Playgroud)
Rod*_*nto 15
终于成功了!
这里有一个快速指南,说明你在阅读本文时所做的事情,因为你有类似的问题.(我使用的是Python 3.5.2,Pygame 1.9.2和Pyinstaller 3.2)
就像C._解释另一个答案(谢谢),以防你加载这样的文件:
import os
folder_path = os.path.join(path.dirname(__file__), 'folder')
some_image = pygame.image.load(os.path.join(folder_path, 'some_image.png'))
Run Code Online (Sandbox Code Playgroud)
改为:
import sys
import os
# If the code is frozen, use this path:
if getattr(sys, 'frozen', False):
CurrentPath = sys._MEIPASS
# If it's not use the path we're on now
else:
CurrentPath = os.path.dirname(__file__)
# Look for the 'sprites' folder on the path I just gave you:
spriteFolderPath = os.path.join(CurrentPath, 'sprites')
# From the folder you just opened, load the image file 'some_image.png'
some_image = pygame.image.load(path.join(spriteFolderPath, 'some_image.png'))
Run Code Online (Sandbox Code Playgroud)
这是必需的,因为当您冻结代码时,文件将被移动到与您之前使用的文件夹不同的文件夹.确保对所有文件执行此操作.
这是另一个例子:
if hasattr(sys, '_MEIPASS'): # the same logic used to set the image directory
font = path.join(sys._MEIPASS, 'some_font.otf') # specially useful to make a singlefile .exe
font = pygame.font.Font(font, size)
# Don't ask me the difference between hasattr and getattr because I don't know. But it works.
Run Code Online (Sandbox Code Playgroud)
如果你想要一个不是pyinstaller默认的图标,你可以选择一个png图像并使用一些在线转换器(Google it)将其转换为.ico.之后,将.ico文件放在.py文件所在的文件夹中.
此时,您应该知道是否需要一个自包含的.exe文件或一组单独的文件,您将压缩并发送给人们.在任何情况下,打开.py文件所在文件夹上的终端.
如果您想要一个文件,请使用:
pyinstaller --onefile --icon=icon_file.ico game_file.py
Run Code Online (Sandbox Code Playgroud)
如果不这样做,请使用:
pyinstaller --icon=icon_file.ico game_file.py
Run Code Online (Sandbox Code Playgroud)
如果您现在不想设置图标,请不要使用该--icon部件.您可以稍后更改.你以后不能改变的是--onefile选项(至少据我所知).
将创建名为game_file.spec的文件.它将自动从game_file.py获取名称.如果它们有不同的名称,你可以搞砸它,所以现在不要有创意.如果您选择单个文件,它应如下所示:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['game_file.py'],
pathex=['C:\\some\\path\\The path where your .py and .spec are'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='game_file',
debug=False,
strip=False,
upx=True,
console=True , icon='icon_file.ico')
Run Code Online (Sandbox Code Playgroud)
如果你选择了一堆文件,你会看到这个附加部分:
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='game_file')
Run Code Online (Sandbox Code Playgroud)
在block_cipher = None您要添加游戏加载的文件之后.像这样:
added_files = [
( 'a folder', 'b folder' ), # Loads the 'a folder' folder (left) and creates
# an equivalent folder called 'b folder' (right)
# on the destination path
( 'level_1/level2', 'level_2' ), # Loads the 'level_2' folder
# that's inside the 'level_1' folder
# and outputs it on the root folder
( 'comic_sans.ttf', '.'), # Loads the 'comic_sans.ttf' file from
# your root folder and outputs it with
# the same name on the same place.
( 'folder/*.mp3', '.') # Loads all the .mp3 files from 'folder'.
]
Run Code Online (Sandbox Code Playgroud)
现在你必须在这里添加'added_files':
a = Analysis(['game_file.py'],
pathex=['C:\\some\\path\\The path where your .py and .spec are'],
binaries=None,
datas=added_files, # Change 'None' to 'added_files' here
# Leave everything else the way it is.
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
Run Code Online (Sandbox Code Playgroud)
您还可以更改一些设置:
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='game_file', # Name of the output file. Equivalent to '--name'
# Don't change it.
debug=False, # If True shows a debug screen on start. Equivalent to '--debug'.
strip=False,
upx=True, # Compresses executable files and libraries
# If console=True, a console screen will be shown on start up.
# icon= is the location of the icon of the exe.
console=True , icon='icon_file.ico')
Run Code Online (Sandbox Code Playgroud)
如果您没有更改exe的路径或输出名称,就像我告诉您不要对注释进行更改,我们只需要运行它,并且将更新先前创建的.exe文件.在命令窗口中输入:
pyinstaller game_file.spec
Run Code Online (Sandbox Code Playgroud)
请记住,这game_file.spec是我们刚刚编辑的文件,'game_file'是我用作示例的随机名称.另请注意,没有,--some_option因为它们不适用于spec文件.这就是您必须直接在脚本中更改它们的原因.--onefile也不能在这里工作,不能在脚本内完成,这就是我之前告诉你的原因.
您将在.spec文件所在的同一文件夹上创建两个文件夹.名为'Dist'的那个包含exe文件,如果你没有使用--onefile,它还应该有一堆其他文件,你需要与exe压缩以与其他人共享应用程序.还会有一个'Buid'文件夹,但我不知道它的用途,因为你不需要它来使用该应用程序.
所以就是这样.它应该适合你.
当我提出问题时我的错误是我不知道该sys._MEIPASS部分(再次感谢C._),我的spec文件的名称与我的py文件不同,我使用'/sprites'而不是'sprites'in added_files而且不知道我被认为运行spec文件而不是py文件.
有关Pyinstaller的更多信息,请查看手册,但由于它远远不够好,有时会产生误导,因此最好使用Google.
| 归档时间: |
|
| 查看次数: |
13215 次 |
| 最近记录: |