Letting user modify the program through a "config" folder

36 views Asked by At

I'm creating an executable file using python (3.11.7)'s pyinstaller (6.3.0). The program relies on a pre-defined dictionary and an Excel file, both located in a folder named config at the top level of my programs directory. I would like both of this elements to be modifiable by the user as follows:

  • There should be a "config" folder
  • where the user can replace the pre-defined Excel file by one of their choosing
  • and edit the python dictionary with a text editor of their liking (note pad?).
  • The changes should remain for future instances of the excecutable.

I have no idea how to achieve this. If I weren't dealing with an executable, I'd simply modify the config folder directly; simple enough. In my actual case, I'm dealing with an executable that will run on someone's else's computer (windows or mac) so I can't just create the folder, let alone make my program find it (as of right now). Even if I could, I doubt that the Executable will get updated to use the new files.

I want a simple way for the user to change the contents of my config folder in my directory after the package is turned into an executable.

1

There are 1 answers

1
Pyenb On

You could look into JSON config files. Python + JSON work really easily together. So you (or the user) could write the path into a config.json file, from which you then read the information on each startup.

Check out this article for example: Reading and writing JSON config files in Python

This could then look something like this:

config.json

{
    "exel_name": "/home/exel.xls",
    "dict_path": "/home/dict.dic"
}

And a config_loader.py like:

import json

with open("config.json", "r") as jsonfile:
    data = json.load(jsonfile)

print(data["exel_name"])
print(data["dict_path"])

Which would load, read and print out the saved paths.

Hope this helps