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?
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
Method-1: Put the picture in the directory of the executable file. First we using the Python package PyInstaller to package our program with picture to the executable file. Then we come to the dist folder. We have an executable file test_GUI.exe, double click and run it.
In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.
The most common reason a PyInstaller package fails is that PyInstaller failed to bundle a required file. Such missing files fall into a few categories: Hidden or missing imports: Sometimes PyInstaller can't detect the import of a package or library, typically because it is imported dynamically.
Use the --icon argument to specify a custom icon for the application. It will be copied into the Resources folder. (If you do not specify an icon file, PyInstaller supplies a file icon-windowed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With