I am trying to create a standalone executable from my Python application using PyInstaller. However, when I run the generated executable, I encounter an error stating that it cannot find the JSON files that are required by my application.
Here is the relevant part of my directory structure:
project_folder/
|
│__main.py
|__wind.py
|__lang.py
|__Core.py (In this file I have methods for loading both .json files)
|
├── source/
│ ├── lang_geo.ico
│ ├── lang_en.ico
│ |__ logo.ico
└── json/
├── sec_db_url.json
└── first_db_url.json
I tried to editing .spec file like this but it didn’t helped:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
datas=[('json/sec_db_url.json', 'json'), ('json/first_db_url.json', 'json')]
a = Analysis(['main.py'],
pathex=['C:\Path\to\My\project_folder'],
binaries=[],
datas=datas,
hiddenimports=[],
hookspath=[],
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,
[],
exclude_binaries=True,
name='App',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas, # Include JSON files
strip=False,
upx=True,
upx_exclude=[],
name='App')
Command I use to create .exe
pyinstaller --onefile --add-data "source;source" --icon="source/logo.ico" main.py
then I edited .spec
pyinstaller main.spec