I've been trying to complete this simple program and turn it into an exe, but every time I run the exe I receive the following error:
Traceback (most recent call last):
File "webtest.py", line 7, in <module>
File "PIL\Image.py", line 3227, in open
FileNotFoundError: [Errno 2] No such file or directory: 'gs-image.png'
**Here is my program **
import webbrowser
import pystray
import PIL.Image
image = PIL.Image.open("gs-image.png")
def on_clicked(icon, item):
if str(item) == "Open GS Digi Ecosystem":
webbrowser.open_new("https://www.google.com/")
elif str(item) == "Close":
icon.stop()
icon = pystray.Icon("test", image, menu=pystray.Menu(
pystray.MenuItem("Open GS Digi Ecosystem", on_clicked),
pystray.MenuItem("Close", on_clicked)
))
icon.run()
Here is the .spec folder
block_cipher = None
a = Analysis(
['webtest.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='webtest',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=['gs-icon.ico'],
)
My images are all in the same folder as the original script and I use the following line to create the exe
pyinstaller -i gs-icon.ico --onefile --noconsole webtest.py
I've tried to add this line to the .spec, but I still get the same error Added Spec line
datas=[('gs-image.png','.')],
then run
pyinstaller webtest.spec
I have noticed, if the image file is in the same file as the exe then the program runs perfectly.
This is because
"gs-image.png"
is a relative path. Relative paths are relative to the users current working directory. When you click to run theexe
file, the current working directory is the directory containing theexe
file. That is why if the image is located in the same directory as the executable your program functions as it should.If you want to reference the image compiled with the program you need to direct your program to look relative to the runtime directory that is created by pyinstaller.
for example: