【问题标题】:Pyinstaller image does not loadPyinstaller 映像无法加载
【发布时间】:2017-05-29 13:12:32
【问题描述】:

问题

我试图将 python 文件转换为 EXE 文件,但是我似乎每次都遇到相同的问题,无论是 CX_Freeze 还是 Pyinstaller。我刚刚尝试使用 pyinstaller 并使用命令制作了一个 EXE 文件

pyinstaller --onefile thepyfile

一切正常。它在 dist 文件中创建 exe。但是,当我打开 exe 时,它​​会显示一个命令窗口,然后快速关闭。我设法捕捉到使用打印屏幕时出现的错误,它说 pygame 错误:无法打开图像 family.jpg。我正在使用 pygame 模块。

我尝试了什么?

我确保图像与我的 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

感谢您的回复

【问题讨论】:

  • 您是否尝试过使用 pyinstaller one directory 选项并验证您需要的所有文件都在目录中?
  • 重新开始,要调试它,您应该在构建期间使用一个目录选项。去阅读那个选项。这是这里的第二个要点:github.com/pyinstaller/pyinstaller/wiki/…,标志在这里:pyinstaller.readthedocs.io/en/stable/…
  • 我以前做过这个,我从你的脚本中看到的唯一主要区别是你在"include_files" 中包含了单个文件。我总是将所有文件(源文件除外)放在一个名为 Data 的文件夹中,然后执行 "include_files":["Data"]。不过,您必须更改程序代码才能从 Data 文件夹加载文件。
  • 命令os.path.join("family.jpg") 是无用的,因为它给出了结果"family.jpg"。要创建完整路径,您需要 os.path.join(path_to_folder, "family.jpg")
  • 我按照你说的做了,并为图片创建了一个单独的文件夹,我确保它在常规 py 中可以正常工作,但是在我把它变成一个 exe 之后,它现在是 pygame 错误:无法打开文件夹/文件。

标签: python pygame pyinstaller cx-freeze


【解决方案1】:

如果 pyinstaller 捆绑在您创建单文件夹捆绑包时有效(删除 --onefile 参数),那么问题可能是这样的:

当您运行一个文件包时,会创建一个临时文件夹结构。临时文件夹的名称是在运行时创建的,捆绑时不知道。因此路径是未知的。

然而,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 docu

【讨论】:

  • 说系统没有属性_MEIPASS
  • 那么你不运行 Pyinstaller 捆绑版本。见上文如何使其运行“捆绑”和“本机”。
  • 嗯,我没有得到我需要的答案,但是自从赏金结束,你是最有帮助的,享受吧。
  • 嘿,谢谢!关心“问题状态”的更新?我认为 Pyinstaller 可以使用更多记录在案的问题的解决方案。
【解决方案2】:

您可以使用 py2exe 将 python 转换为可执行文件。到目前为止,这种方法对我很有效。

pte.pyC:\Python\toexe>中的脚本文件

  1. 导航到C:\Python\toexe>
  2. xxx.py python 文件放在同一目录中
  3. 编辑pte.py文件如下:

    ------------------------------------------

    从 distutils.core 导入设置

    导入py2exe

    setup(console=['xxx.py']) ## 用于 CLI 程序

    setup(windows=['xxx.py']) ### for gui

    ------------------------------------------

  4. 执行此命令:C:\Python\toexe> pte.py py2exe

【讨论】:

  • 不,我使用的是 python 3.5
【解决方案3】:

看线:

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

include_files 部分在基本目录中指定两个文件,而不是在您的单独文件夹中。

尝试类似:

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"))]}},

【讨论】:

  • 谢谢你的回答,我会尝试改变一些东西,看看它是否有效。稍后评论
  • 您愿意在“Treg&Shelly.py”中发布您的代码吗?特别是错误消息中第 53 行附近的代码。在打包过程中是否有任何关于family.jpg的消息?更具体地说,您是否看到说明图片已包含在内的消息?
  • 另外,出于好奇,您的 PATH 环境变量设置为什么?
  • 那将是 "C:\Users\person\Documents\Python\Cards\projectfolder\images" 并且错误行 53 使用 - python_powered = pygame.image.load(os.path. join('images',"family.jpg")).convert() 当我运行它时,它运行良好,并且我确实看到消息正在构建包含图像的消息。现在好像不包括我的文件夹了?
  • python_powered = pygame.image.load(os.path.join(os.getcwd(), "images","family.jpg")).conve‌​rt() 会改变行为吗?
【解决方案4】:

由于您的冻结程序不知道它在哪个文件夹中,您应该使用os.getcwd()

python_powered = pygame.image.load(os.path.join(os.getcwd(),'images',"family.jpg")).conve‌​rt()

当然不要忘记import os

【讨论】:

    【解决方案5】:

    提出的解决方案:

     pyinstaller --noconfirm --onefile --windowed   --splash image.jpg file.py after adding this you can import a py_splash after that used py_splash.close()
    

    【讨论】:

      【解决方案6】:

      只需在python文件位置的cmd提示符下输入pip install pygame,希望对你有帮助!

      【讨论】:

      • 它已经安装在python目录位置,不过感谢您的建议。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      相关资源
      最近更新 更多