Home > Mobile >  Merge a Json and python file into a single .EXE
Merge a Json and python file into a single .EXE

Time:09-17

I have a python script that reads information from a json file and uses it to set the values of the variables at the beginning of the script. The problem is that when I generate the .exe file from the python script, I need to have the .json file in the same folder as the .exe file in order to read and write to it.

The python script implements a GUI that allows the user to change the value of the json file so the next time that the user opens the executable file, the values of the variable will be updated.

Is there any possibility to avoid using a config.json file or merge both .json and .py files into one single .exe?

I would like to have only one .exe file and the only way to change the values for future executions to be through the GUI.

I have a dictionary of dictionaries in the cofig.json and this is how I read the data and write it when the user changes it:

import json
# Reading the config.json file
f = open('config.json')
data = json.load(f)
version_data = data['version_data']
software_dict = data['software_dict']
paths_dict = data['paths_dict']

# Then when I change the version_data, software_dict or paths_dict value I write it in the json file
with open('config.json', 'w', encoding='utf-8') as f:
    f.write(json.dumps({'version_data':version_data, 'software_dict':software_dict, 'paths_dict': paths_dict}, indent=4)
f.close()

And the pyinstaller command that I use to create the executable is:

pyinstaller --onefile --noconsole "python_script.py"

CodePudding user response:

If you're on windows, without invoking a cross-platform online API solution, you might consider utilizing the /appdata/ folder. It would still require a JSON file, but it wouldn't be visible to the user.

  • Related