Pyinstaller image does not load
Asked Answered
P

8

17

The Problem

Im trying to turn a python file into an EXE file, however I seem to be running into the same problem every single time, whether it is CX_Freeze or Pyinstaller. I just tried using pyinstaller and I made an EXE file using the command

pyinstaller --onefile thepyfile

and everything works fine. It creates the exe in the dist file. However when I open the exe it shows me a command window and then quickly turns off. I managed to capture the error im getting using print screen, and it said pygame error: Couldn't open image family.jpg. I am using the pygame module.

What have I tried?

Iv made sure that the images are in the same directory and the same folder as my python file. My .py works fine when I run it, it's just the exe. Anyways just to make sure that there's no problems loading the images in the path I joined the path using

os.path.join

Again it worked for the py file, but it did not work in the exe. I have also checked if I installed pyinstaller correctly, and it works for other exe programs that don't involve importing images. I also did try to create a folder and then use

os.path.join(folder,file)

but again it worked in the py file, but not the pyinstaller/cx_freeze exe.

A clue?

While I was working with CX__freeze I discovered pygame cant import the image either. However it gave me a larger error case, not sure if it's usefull but, it may be a clue?

enter image description here

Please Help

Iv been running into this problem for more than 5 weeks now and am desperate for help.

Some Code

This is how I import the image (again works in the py file but not the exe)

family_image = pygame.image.load(os.path.join('folder',"family.jpg")).convert()

And if needed heres my cx_Freeze setup.py which also makes the exe file yet gives me image cant be loaded error.

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

    )

Note

All my images are in a separate folder but are accessible by my python file.

Also Im using python 3.5

Thank you for any response

Pugliese answered 14/1, 2017 at 4:48 Comment(8)
Have you tried using the pyinstaller one directory option and validating that all file you need are in the directory?Benitabenites
Starting over, to debug this you should use the one directory option during the build. Go read up on that option. It is the second bullet point here: github.com/pyinstaller/pyinstaller/wiki/… and the flag is here: pyinstaller.readthedocs.io/en/stable/…Benitabenites
I've done this before, and the only major difference I see from your script is that you are including individual files in the "include_files". I always stick all of my files(other than the source) in a folder named Data and just do "include_files":["Data"]. You would have to change your program's code to load files from the Data folder though.Whimsical
command os.path.join("family.jpg") is useless because it gives result "family.jpg". To create full path you need os.path.join(path_to_folder, "family.jpg")Kiser
I did as you said and created a seperate folder for the images, I made sure it worked in the regular py and it does, however after I turn it into an exe, it now sais pygame error: couldn't open folder/file.Pugliese
Yes exactly. Now, is the file you need present in the one directory? If it is, then the error is in the code that tries to find the file. If the file is absent, then the error happens when pyinstaller tries to figure out what files are needed.Benitabenites
It seems the file is not present in the one directory. I checked in the included files folder in the onedir folder. How could I fix this error in finding the files needed?Pugliese
absolute path, full path ? replace all open(xx.yy) to open(os.getcwd()+"\""+ filename). You can't use static path cos user can change it.Podesta
H
2

In case the pyinstaller bundling works if you create a one-folder bundle (remove the --onefile parameter) then the issue is probably this:

When you run a one-file bundle, a temporary folder-structure is created. The name of the temporary folder is created at run-time and not known when you bundle it. Therefore the path is not known.

Pyinstaller however adds an attribute sys._MEIPASS which contains the absolute path to the temporary folder. So, try something like:

if getattr(sys, 'frozen', False):
    wd = sys._MEIPASS
else:
    wd = ''    
family_image = pygame.image.load(os.path.join(wd,'folder',"family.jpg")).convert()

Also see ther Pyinstaller docu.

Halidom answered 21/1, 2017 at 15:52 Comment(4)
It sais sys has no attribute _MEIPASSPugliese
Then you do not run the Pyinstaller bundled version. See above how to make it run "bundled" and "native".Halidom
Eh I didnt get the answer I needed but since the bounties over and you were the most helpfull, enjoy.Pugliese
Hey, thanks! Care update on the "state of the problem"? I think Pyinstaller could use a lot more of documented problems with solutions.Halidom
G
1

You could try to add the images to the same path as the exe file. It worked for me.

Gerkman answered 20/12, 2023 at 23:23 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Rubberneck
W
0

You can use py2exe to convert python to an executable. This approach has so far it worked for me.

pte.py is the script file in C:\Python\toexe>

  1. Navigate to C:\Python\toexe>
  2. Place your xxx.py python file in the same directory
  3. Edit the pte.py file as below:

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

    from distutils.core import setup

    import py2exe

    setup(console=['xxx.py']) ## for CLI programms

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

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

  4. Execute this command: C:\Python\toexe> pte.py py2exe

Westonwestover answered 18/1, 2017 at 18:3 Comment(1)
Nope, Im using python 3.5Pugliese
A
0

Looking at the line:

options = {"build_exe":{"packages":["pygame","random","math"],"excludes" : ['tcl','ttk','tkinter','Tkinter'],"include_files":["family.jpg","newyears.png"]}},

Consider:

All my images are in a separate folder

The include_files section is specifying two files in the base directory, not in your separate folder.

Try something like:

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

Notably, this will place the files in the base directory. Consider providing a tuple of (input, output) for each file instead of just input to specify where exactly you want those files to go.

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

Accede answered 19/1, 2017 at 0:0 Comment(5)
Thank you for the answer, I'll try to change some stuff and see if it works. Ill comment laterPugliese
Would you be open to posting the code you have in "Treg&Shelly.py"? In particular, the code around line 53 from the error message. Was there any messages regarding family.jpg at all during the packaging process? More specifically, do you see messages saying the image was included?Accede
Also, out of curiosity, what is your PATH environmental variable set to?Accede
That would be "C:\Users\person\Documents\Python\Cards\projectfolder\images" and error line 53 uses- python_powered = pygame.image.load(os.path.join('images',"family.jpg")).convert() When I run it it works fine, and I do see messages as im building that the images were included. It seems not to be including my folder now?Pugliese
Does python_powered = pygame.image.load(os.path.join(os.getcwd(), "images","family.jpg")).conve‌​rt() change the behavior?Accede
V
0

As your frozen program doesn't know in which folder it is, you should use os.getcwd() :

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

Off course don't forget import os

Vestment answered 10/2, 2017 at 1:50 Comment(0)
Q
0

Solution proposed:

 pyinstaller --noconfirm --onefile --windowed   --splash image.jpg file.py after adding this you can import a py_splash after that used py_splash.close()
Quarrel answered 20/12, 2021 at 9:48 Comment(0)
H
0

I have an alternative solution. Just delete dirs created by pyinstaller and use auto-py-to-exe

Install auto-py-to-exe in your project via CLI:

pip install auto-py-to-exe

then

auto-py-to-exe

you will see auto-py-to-exe gui

click in tab named Additional Files, click add folder, and select folder, and add . at the end of image folder path

click to see how to set

Heptagon answered 24/7, 2022 at 15:9 Comment(0)
S
-1

just type in pip install pygame into the cmd prompt of the file location of python I hope this helped!

Strangles answered 23/10, 2017 at 16:2 Comment(1)
It already is installed in the python directory location, thanks for the advice though.Pugliese

© 2022 - 2025 — McMap. All rights reserved.