{"id":342,"date":"2023-08-04T15:23:02","date_gmt":"2023-08-04T15:23:02","guid":{"rendered":"https:\/\/python.garden\/?p=342"},"modified":"2023-08-07T16:17:31","modified_gmt":"2023-08-07T16:17:31","slug":"using-a-config-file-with-a-python-script","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/08\/04\/using-a-config-file-with-a-python-script\/","title":{"rendered":"Using a Config File with a Python Script"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A configuration file (or config file) is a file used to configure the initial settings for some computer programs. In Python, using a configuration file can significantly simplify the management of settings and parameters for your scripts, especially in a complex software system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Different file formats can be used for config files in Python, such as <code>.ini<\/code>, <code>.json<\/code>, <code>.yaml<\/code> and <code>.cfg<\/code>. In this article, we&#8217;ll explore how to use each of these formats and some modules in Python that facilitate the reading and writing of config files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ConfigParser Module<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ConfigParser<\/code> is a built-in Python module for reading <code>.ini<\/code>-style configuration files. It provides a way of reading and writing data from and to <code>.ini<\/code> files, which are a standard for config files in Windows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Config File in .ini Format<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;properties&quot;,&quot;mime&quot;:&quot;text\/x-properties&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Properties files&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;properties&quot;}\">[DEFAULT]\nserver = localhost\n\n[web_server]\nhost = 127.0.0.1\nport = 80\n\n[db_server]\nhost = 127.0.0.1\nport = 3306\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to Read the Config File<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import configparser\n\nconfig = configparser.ConfigParser()\nconfig.read('config.ini')\n\nprint(config['DEFAULT']['server'])  # localhost\nprint(config['web_server']['host'])  # 127.0.0.1\nprint(config['db_server']['port'])  # 3306<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">JSON<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>JSON<\/code> files are another common format for config files, and Python&#8217;s <code>json<\/code> module makes it easy to read and write these files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Config File in .json Format<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application\/json&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JSON&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;json&quot;}\">{\n  &quot;server&quot;: &quot;localhost&quot;,\n  &quot;web_server&quot;: {\n    &quot;host&quot;: &quot;127.0.0.1&quot;,\n    &quot;port&quot;: 80\n  },\n  &quot;db_server&quot;: {\n    &quot;host&quot;: &quot;127.0.0.1&quot;,\n    &quot;port&quot;: 3306\n  }\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to Read the Config File<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import json\n\nwith open('config.json', 'r') as f:\n    config = json.load(f)\n\nprint(config['server'])  # localhost\nprint(config['web_server']['host'])  # 127.0.0.1\nprint(config['db_server']['port'])  # 3306<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">YAML<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>YAML<\/code> is a human-friendly data serialization standard and it&#8217;s often used for writing configuration files. You can use the <code>PyYAML<\/code> module to work with YAML files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Config File in .yaml Format<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text\/x-yaml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;YAML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;yaml&quot;}\">server: localhost\n\nweb_server:\n  host: 127.0.0.1\n  port: 80\n\ndb_server:\n  host: 127.0.0.1\n  port: 3306<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to Read the Config File<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import yaml\n\nwith open('config.yaml', 'r') as f:\n    config = yaml.safe_load(f)\n\nprint(config['server'])  # localhost\nprint(config['web_server']['host'])  # 127.0.0.1\nprint(config['db_server']['port'])  # 3306<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Environment Variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For sensitive data, like usernames and passwords, you may want to use environment variables instead of plain text configuration files. The <code>os<\/code> module in Python allows you to access these environment variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to Set Environment Variables in Python<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import os\n\nos.environ['USER'] = 'my_user'\nos.environ['PASSWORD'] = 'my_password'<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to Get Environment Variables in Python<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import os\n\nuser = os.getenv('USER')\npassword = os.getenv('PASSWORD')\n\nprint(user)  # my_user\nprint(password)  # my_password<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In conclusion, Python provides a variety of methods and modules to handle configuration files in different formats. This flexibility allows you to choose the best approach according to your project requirements, making your code more modular, secure, and easier to manage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A configuration file (or config file) is a file used to configure the initial settings for some computer programs. In Python, using a configuration file can significantly simplify the management&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[214,213],"tags":[],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-beginners-guide","category-python"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}