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
one directory
option and validating that all file you need are in the directory? – Benitabenites"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. – Whimsicalos.path.join("family.jpg")
is useless because it gives result"family.jpg"
. To create full path you needos.path.join(path_to_folder, "family.jpg")
– Kiseropen(xx.yy)
toopen(os.getcwd()+"\""+ filename)
. You can't use static path cos user can change it. – Podesta