Python Forum
variables help / update config file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: variables help / update config file (/thread-14383.html)



variables help / update config file - mapvis - Nov-27-2018

In my python script I have way too many default variables / parameter options. What is the best practice for moving these default variables into a config file that the users can update? This config file will be global variables that my main script will read and apply each time it's run.

config file:
x = 1
y = 2

main script:
import config file
sum = x + y

Please comment with the best practices for dealing with updateable config files for variables so the default values arent used each time. Thanks.


RE: variables help / update config file - buran - Nov-27-2018

Check configparser module from Standard Python Library
Tutorial to work with configparser on PyMOTW3


RE: variables help / update config file - Gribouillis - Nov-27-2018

Besides Buran's suggestion to use configparser, your program can have a strategy such as:
  • If the program is called with -c or --config option, use the config file given on the command line
  • Otherwise use myprog.cfg file from the current directory
  • Otherwise use a user default config file such as ~/.config/myprog/default.cfg
  • Otherwise use a system-wide default config file ...
There are many options for this.


RE: variables help / update config file - mapvis - Nov-27-2018

Thanks! i will try the configparser.