问题
我正在尝试将 python 文件转换为 EXE 文件,但是我似乎每次都会遇到同样的问题,无论是 CX_Freeze 还是 Pyinstaller。我刚刚尝试使用 pyinstaller 并使用命令制作了一个 EXE 文件
pyinstaller --onefile thepyfile
一切正常。它在 dist 文件中创建 exe。但是,当我打开 exe 时,它会显示一个命令窗口,然后很快关闭。我设法捕获了使用打印屏幕时遇到的错误,它说 pygame错误:无法打开图像family.jpg。我正在使用 pygame 模块。
我尝试了什么?
Iv 确保图像与我的 python 文件位于同一目录和同一文件夹中。我的 .py 在运行时工作正常,它只是 exe。无论如何,只是为了确保在我使用
加入路径的路径中加载图像没有问题os.path.join
它再次适用于 py 文件,但在 exe 中不起作用。我还检查了我是否正确安装了 pyinstaller,它适用于其他不涉及导入图像的 exe 程序。我也尝试创建一个文件夹然后使用
os.path.join(folder,file)
但它再次在 py 文件中起作用,但在 pyinstaller/cx_freeze exe 中不起作用。
有线索吗?
当我使用 CX__freeze 时,我发现 pygame 也无法导入图像。然而它给了我一个更大的错误案例,不确定它是否有用,但是,它可能是一个线索?
请帮忙
我遇到这个问题已经超过 5 周了,非常需要帮助。
一些代码
这就是我导入图像的方式(同样适用于 py 文件,但不适用于 exe)
family_image = pygame.image.load(os.path.join('folder',"family.jpg")).convert()
如果需要的话,这里是我的cx_Freeze setup.py,它也会生成exe文件,但会给我图像无法加载错误。
import cx_Freeze
import sys
import pygame
import random
import math
import os
os.environ['TCL_LIBRARY'] = "C:\\Program Files\\Python35\\tcl\\tc18.6"
os.environ['TK_LIBRARY'] = "C:\\Program Files\\Python35\\tcl\\tk8.6"
base = None
if sys.platform == 'win32':
base = "Win32GUI"
executables = [cx_Freeze.Executable("Treg&Shelly.py",shortcutName="Cards",shortcutDir="DesktopFolder",base = base)]
cx_Freeze.setup(
name = "HAPPY NEW YEARS",
options = {"build_exe":{"packages":["pygame","random","math"],"excludes" : ['tcl','ttk','tkinter','Tkinter'],"include_files":["family.jpg","newyears.png"]}},
version = "0.01",
description = "New Years Card",
executables = executables
)
注意
我的所有图像都位于单独的文件夹中,但可以通过我的 python 文件访问。
我也使用python 3.5
感谢您的回复
如果您创建单文件夹捆绑包(删除 --onefile 参数),pyinstaller 捆绑有效,那么问题可能是这样的:
当您运行单文件包时,会创建一个临时文件夹结构。临时文件夹的名称是在运行时创建的,在捆绑时不知道。因此路径未知。
然而,Pyinstaller 添加了一个属性sys._MEIPASS
,其中包含临时文件夹的绝对路径。所以,尝试这样的事情:
if getattr(sys, 'frozen', False):
wd = sys._MEIPASS
else:
wd = ''
family_image = pygame.image.load(os.path.join(wd,'folder',"family.jpg")).convert()
另请参阅
Pyinstaller 文档。
pte.py
是
C:\Python\toexe>
中的脚本文件
C:\Python\toexe>
xxx.py
python 文件放在同一目录中
pte.py
文件如下:---------------------------------------------------------
导入py2exe
setup(console=['xxx.py']) ## 用于 CLI 程序
setup(windows=['xxx.py']) ### for gui
---------------------------------------------------------
C:\Python\toexe> pte.py py2exe
options = {"build_exe":{"packages":["pygame","random","math"],"excludes" : ['tcl','ttk','tkinter','Tkinter'],"include_files":["family.jpg","newyears.png"]}},
All my images are in a separate folder
尝试类似:
options = {"build_exe":{"packages":["pygame","random","math"],"excludes" : ['tcl','ttk','tkinter','Tkinter'],"include_files":[os.path.join("imageDir", "family.jpg"),os.path.join("imageDir","newyears.png")]}},
options = {"build_exe":{"packages":["pygame","random","math"],"excludes" : ['tcl','ttk','tkinter','Tkinter'],"include_files":[(os.path.join("imageDir", "family.jpg"), os.path.join("imageDir", "family.jpg")),(os.path.join("imageDir","newyears.png"),os.path.join("imageDir","newyears.png"))]}},
os.getcwd() :
python_powered = pygame.image.load(os.path.join(os.getcwd(),'images',"family.jpg")).convert()
当然别忘了
import os
pyinstaller --noconfirm --onefile --windowed --splash image.jpg file.py after adding this you can import a py_splash after that used py_splash.close()