Where to put the config file for the compiled Python script?

It is similar to Where to put the config file in Python? but I am asking about scripts compiled / frozen with py2exe or similar on windows systems. (Namely, this one: What config file format should I use for convenience strings of arbitrary bytes? <a2> )

My first thought was to put the config file in the same folder as the .exe, making it self-contained. But if I link the files to the .exe, it will be called from their directory, not mine, so I need something like How do I get the path to the current executable in Python? to find the config file.

Is this the best way? Or are there some standard config files to search for, like this StackOverflow question ?

+3


source to share


4 answers


You can use the appdirs module to define an appropriate folder for user data.



+5


source


If this is a config file that the user needs to edit, then put it somewhere explicitly. I tend to "compile" python applications in a directory structure like this:

C:/path/to/Application
| config.ini
| Run Application.bat  (runs code/application.exe)
| code/
  | application.exe
  | library.zip 
  | ... etc. 

      



This allows you to save the "run application" shortcut and configuration file in the same location that the user can easily find.

+2


source


If you are using py2exe to package your script, following their links for the one that describes how to figure out the path to the executable This seems to be the correct path. If you decide to keep your config file relative to the EXE, it's up to you. You can either place it in the root of your application directory (next to the exe) or create a config subdirectory. This is your choice.

On linux machines it is sometimes common to use the location etc/

for the configuration files. OSX, you would either use the user preference location or something else. But again, on any of these platforms, you can also use a relative conf file in the application directory.

If you are packaging some type of GUI then you can use the registry and offer configuration options through your interface.

+1


source


I created "compiled" executables in python in widnows that have file types associated with it, and I never had a problem opening my config file that was placed in the same location as the .exe. I used something like this to find it:

    conf = open(r"%s\settings.conf" % os.getcwd())

      

I kept my values ​​separated by newline, so the file looked like this:

PNelson
21
6'

      

The username, age and height are stored here to read the values, which I would use like this:

settings = "\n".split(conf.read())

      

Then I checked the length of the array to make sure the config file was not corrupted or somehow, if it were, I would write the defaults to the file and use them.

0


source







All Articles