Python Forum

Full Version: variables help / update config file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Check configparser module from Standard Python Library
Tutorial to work with configparser on PyMOTW3
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.
Thanks! i will try the configparser.