Last modified: Apr 10, 2026 By Alexander Williams

Python ConfigParser vs Argparse Guide

Python offers many tools for handling configuration. Two popular ones are ConfigParser and Argparse.

They solve different problems. Knowing when to use each is a key skill.

This guide explains their purposes, shows examples, and helps you choose the right tool.

What is ConfigParser?

ConfigParser reads and writes configuration files. These files store settings for your application.

Think of files like config.ini or settings.conf. They keep data separate from your code.

This is useful for database connections, API keys, or user preferences. You change the file, not the script.

Basic ConfigParser Example

Let's create a simple config file and read it with Python.

First, create a file named app_config.ini.


[Database]
host = localhost
port = 5432
name = mydb
user = admin

[Settings]
debug_mode = true
log_level = INFO
    

Now, use Python's configparser module to read this file.


import configparser

# Create a ConfigParser object
config = configparser.ConfigParser()

# Read the configuration file
config.read('app_config.ini')

# Access values from the 'Database' section
db_host = config['Database']['host']
db_port = config['Database'].getint('port')  # Get as integer

# Access values from the 'Settings' section
debug_mode = config['Settings'].getboolean('debug_mode')
log_level = config['Settings']['log_level']

print(f"Database Host: {db_host}")
print(f"Database Port: {db_port}")
print(f"Debug Mode: {debug_mode}")
print(f"Log Level: {log_level}")
    

Database Host: localhost
Database Port: 5432
Debug Mode: True
Log Level: INFO
    

The ConfigParser class makes it easy. You can get strings, integers, and booleans.

Your settings stay safe in a file. This is great for persistent, file-based configuration.

What is Argparse?

Argparse is for command-line arguments. It lets users pass options when they run your script.

For example, python script.py --input file.txt --verbose. Argparse parses these inputs.

It creates help messages and validates data. It's the standard way to build command-line tools in Python.

Basic Argparse Example

Here is a simple script that uses argparse.


import argparse

# Set up the argument parser
parser = argparse.ArgumentParser(description='Process some files.')

# Add arguments
parser.add_argument('--input', '-i', help='Path to the input file', required=True)
parser.add_argument('--output', '-o', help='Path to the output file')
parser.add_argument('--verbose', '-v', action='store_true', help='Enable verbose output')

# Parse the arguments from the command line
args = parser.parse_args()

# Use the arguments
print(f"Input file: {args.input}")
if args.output:
    print(f"Output file: {args.output}")
print(f"Verbose mode: {args.verbose}")
    

Run the script from the terminal to see it work.


$ python my_script.py -i data.txt -o result.txt -v
    

Input file: data.txt
Output file: result.txt
Verbose mode: True
    

The ArgumentParser class handles everything. It's perfect for user-provided, runtime options.

For more complex needs, you can learn about Python Argparse optional arguments or how to set a Python Argparse default value.

Key Differences: ConfigParser vs Argparse

Source of Configuration

ConfigParser reads from a static file on disk.

Argparse reads from the dynamic command line.

Primary Use Case

Use ConfigParser for settings that change rarely. Like server addresses or file paths.

Use Argparse for options that change every time you run the script. Like input filenames or flags.

Data Persistence

ConfigParser data is saved. The file remains for the next run.

Argparse data is for one session only. It is not saved automatically.

User Interaction

ConfigParser is indirect. An admin edits a file.

Argparse is direct. The user types commands.

When to Use ConfigParser

Choose ConfigParser in these situations.

You need to store secrets or environment-specific data. Keep them out of your code.

Your application has many settings. A file is easier to manage than long command lines.

Settings are shared between multiple scripts or tools. One config file can serve many.

When to Use Argparse

Choose Argparse in these situations.

You are building a command-line tool or utility. Users expect command options.

The script needs different behavior each time it runs. Like processing different files.

You want automatic help text and error checking. Argparse provides this for free.

For advanced patterns, see our Python Argparse example for command line arguments.

Can You Use Them Together?

Yes. This is a powerful pattern.

Use ConfigParser for default, permanent settings. Use Argparse to let users override them at runtime.

Combined Example

This script reads a default config file but allows command-line overrides.


import configparser
import argparse

# 1. Read default configuration from file
config = configparser.ConfigParser()
config.read('defaults.ini')
default_host = config['Server']['host']
default_port = config['Server'].getint('port')

# 2. Set up command-line arguments to potentially override defaults
parser = argparse.ArgumentParser(description='Start the application server.')
parser.add_argument('--host', help=f'Server host (default: {default_host})')
parser.add_argument('--port', type=int, help=f'Server port (default: {default_port})')

args = parser.parse_args()

# 3. Use command-line argument if provided, otherwise use config file value
final_host = args.host if args.host else default_host
final_port = args.port if args.port else default_port

print(f"Starting server on {final_host}:{final_port}")
    

Run it with or without arguments.


$ python server.py
Starting server on localhost:8080

$ python server.py --host 192.168.1.5 --port 9000
Starting server on 192.168.1.5:9000
    

This gives you flexibility. You have sensible defaults but can change them easily.

Conclusion

ConfigParser and Argparse are both essential. They are not competitors.

ConfigParser manages static, file-based settings. It is for configuration that lives with your application.

Argparse manages dynamic, command-line arguments. It is for options provided when a tool is run.

Use ConfigParser for your database URL. Use Argparse for the user's input filename.

For complex command-line interfaces, explore guides on Python Argparse boolean flags.

Choose the right tool for the job. Your code will be cleaner and more professional.