Python Forum
variables help / update config file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variables help / update config file
#1
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.
Reply
#2
Check configparser module from Standard Python Library
Tutorial to work with configparser on PyMOTW3
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
Thanks! i will try the configparser.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File path by adding various variables Mishal0488 2 966 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,016 Jan-21-2023, 11:33 PM
Last Post: SamLiu
  Is it possible to update a CSS file from Python bigAL_python 4 2,197 Apr-20-2022, 01:38 PM
Last Post: bigAL_python
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,695 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  value null when update in json file 3lnyn0 6 3,082 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  Updating a config file [solved] ebolisa 8 2,532 Nov-04-2021, 10:20 AM
Last Post: Gribouillis
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,411 Oct-21-2021, 03:29 AM
Last Post: buran
  Is there a library for recursive object creation using config objects johsmi96 0 1,823 May-03-2021, 08:09 PM
Last Post: johsmi96
  help with pytesseract.image_to_string(savedImage, config='--psm 11')iamge to string korenron 0 2,649 Apr-29-2021, 10:08 AM
Last Post: korenron
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,627 Dec-16-2020, 05:26 AM
Last Post: Vokofe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020