简体   繁体   English

将 Json 和 python 文件合并为一个.EXE

[英]Merge a Json and python file into a single .EXE

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.我有一个 python 脚本,它从 json 文件中读取信息,并使用它来设置脚本开头的变量值。 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.问题是,当我从 python 脚本生成.exe文件时,我需要将.json文件放在与.exe文件相同的文件夹中,以便对其进行读写。

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. python 脚本实现了一个 GUI,允许用户更改 json 文件的值,以便下次用户打开可执行文件时,变量的值将被更新。

Is there any possibility to avoid using a config.json file or merge both .json and .py files into one single .exe ?是否有可能避免使用config.json文件或将.json.py文件合并到一个.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.我希望只有一个.exe文件,并且是通过 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:我在 cofig.json 中有一个字典字典,这就是我在用户更改数据时读取和写入数据的方式:

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 命令是:

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

If you're on windows, without invoking a cross-platform online API solution, you might consider utilizing the /appdata/ folder.如果您使用的是 windows,而不调用跨平台在线 API 解决方案,您可以考虑使用 /appdata/ 文件夹。 It would still require a JSON file, but it wouldn't be visible to the user.它仍然需要一个 JSON 文件,但它不会对用户可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM