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

abc1234
  • 260
  • 3
  • 19
  • 1
    Have you tried using the pyinstaller `one directory` option and validating that all file you need are in the directory? – Stephen Rauch Jan 14 '17 at 04:50
  • 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: https://github.com/pyinstaller/pyinstaller/wiki/How-to-Report-Bugs#make-sure-everything-is-packaged-correctly and the flag is here: http://pyinstaller.readthedocs.io/en/stable/usage.html?highlight=--onedir – Stephen Rauch Jan 14 '17 at 19:20
  • 1
    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. – Larry McMuffin Jan 15 '17 at 01:34
  • 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")` – furas Jan 15 '17 at 16:34
  • 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. – abc1234 Jan 16 '17 at 00:49
  • 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. – Stephen Rauch Jan 16 '17 at 01:02
  • 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? – abc1234 Jan 16 '17 at 01:26
  • 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**. – dsgdfg Jan 17 '17 at 10:56

7 Answers7

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.

hvwaldow
  • 1,296
  • 1
  • 11
  • 13
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

BenH
  • 9,766
  • 1
  • 22
  • 35
OzizLK
  • 47
  • 2
  • 11
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"))]}},

tabbek
  • 866
  • 5
  • 9
  • Thank you for the answer, I'll try to change some stuff and see if it works. Ill comment later – abc1234 Jan 19 '17 at 00:05
  • 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? – tabbek Jan 20 '17 at 02:11
  • Also, out of curiosity, what is your PATH environmental variable set to? – tabbek Jan 20 '17 at 02:18
  • 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? – abc1234 Jan 20 '17 at 02:34
  • Does `python_powered = pygame.image.load(os.path.join(os.getcwd(), "images","family.jpg")).conve‌​rt()` change the behavior? – tabbek Jan 20 '17 at 09:46
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

VdF
  • 34
  • 5
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()
GeralexGR
  • 2,973
  • 6
  • 24
  • 33
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

-1

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

G. Jim
  • 33
  • 10