execution of file with imagen doesn't work with command --onefile in pyinstaller

32 views
Skip to first unread message

Hugo Benavides

unread,
May 2, 2020, 6:15:59 AM5/2/20
to PyInstaller
hi,
I'm a beginner student in Python and PyInstaller but after go accros new and differrent instructions, I've gotten a problem creating an executable using the command pyinstaller at the time of use the helper --onefile

I've created an executable using the next instruction:

          pyinstaller --add-data "Rute PC to my Folder\Imagen";"Imagen"  Aplicacion_Calculadora.py 

The folder Imagen has an imagen that is called into the code and at this time everything works fine, the executable starts and works very fine. I 've used the app and operations are correct and the imagen is upload in the interface, but I deleted everything and started again.

I would like to add everything in one File using the command:

          pyinstaller --onefile --add-data "Rute PC to mi Folder\Imagen";"Imagen"  Aplicacion_Calculadora.py

At this point, the executable never starts. I saw the message in console when the .exe is running and it shows me the next error:

          File "tkinter\__init__.py", line 4061, in __init__
          File "tkinter\__init__.py", line 4006, in __init__
          _tkinter.TclError: couldn't open "./Imagen/Retroceder.png": no such file or directory [11320] Failed to execute script Aplicacion_Calculadora

The executable never can find the folder with the imagen, it happenning just when I use the command --onefile

I've been looking in many documentation and instructions but I've not found anything about that error just using the command --onefile


May you help me with that error or what instruction I should add, please?

Attach code and folder with the imagen


Thanks

Python installed 3.8
pyinstaller executed
Aplicacion_Calculadora.zip

Brenainn Woodsend

unread,
May 2, 2020, 9:51:38 AM5/2/20
to pyins...@googlegroups.com
Hi,

So as you've probably worked out, it's the following line that needs to change:

imagenRetro = PhotoImage(file="./Imagen/Retroceder.png") #llama y carga la imagen

This filename is relative to the current working directory which means that it will break even in normal Python if you run from a different folder. You want to be able to run your code from any folder before you start using PyInstaller. i.e. All of the following should all work without any issues:

python Aplicacion_Calculadora.py

cd ..
python folder
/Aplicacion_Calculadora.py

cd somewhere/random/on/your/machine
python
/full/path/to/Aplicacion_Calculadora.py

To fix this not in PyInstaller you normally use the __file__ variable. Something allong the lines of:

from os import path

script_folder = path.abspath(path.dirname(__file__))

imagenRetro=PhotoImage(file=path.join(script_folder, "Imagen/Retroceder.png"))#llama y carga la imagen


But __file__ is strange inside PyInstaller so PyInstaller has its own mechanism for getting the folder your code is in. It's documented here. Your code would look like:

from os import path

import sys

bundle_dir = getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__)))

imagenRetro=PhotoImage(file=path.join(bundle_dir, "Imagen/Retroceder.png"))#llama y carga la imagen


This fixed it on my machine.

Brénainn

P.S. Thanks for including you code - it makes this so much easier.

--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/1519a9c8-cc73-425c-875c-b02fa5b07619%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages