--- title: "Python Configuration: Top Built-In and Third-Party Libraries" date: "2022-11-10" categories: - "python-tools" coverImage: "woman-configuring-equipment.jpg" --- # Python Configuration: Top Built-In and Third-Party Libraries ## Introduction One of the first things many beginning Python programming tutorials teach is the input function, a simple and beginner-friendly approach to getting input into a program. However, once we get past the thrill of asking ourselves our name so we can change "Hello, world!" into "Hello, Bob!", we realize that configuring a real-world application has many more options than the simple "input" function allows., and that more advanced command line tools usually don't accept input interactively. Instead, at a high level, program configuration often involves some combination of one or more of the following three techniques: - Reading the command line arguments passed to the program. The simplest scripts can simply read `sys.argv` for this. More advanced ones will use Python's argparse module or a third-party command line processor such as Click. - Reading environment variables. This is easy to do by importing the "os" module and reading `os.environ` as though it were a dictionary. - Reading program configuration from a file. In this article, we want to focus primarily on the last item here, configuring a Python program using a file. We'll look at several built-in and third-party options for configuration files, and focus in each case on loading configuration from files rather than building and writing configuration files. ## Best Python Configuration Tools: An Overview Before we begin our detailed discussion of several popular options, let's summarize some of the built-in and third-party libraries that we can use to configure Python applications. We will give examples of some of these in later sections.
Library or Tool | Availability | Pros | Cons |
Simple Python Dictionaries | Built-in | Easy to use and understand. Values are of any type. | No clear separation of configuration and code. |
ConfigParser | Built-in (Standard Library) | Many Python versions supported | Values are always strings. Windows style in files. Somewhat unintuitive. |
Localconfig | PyPi (pip install only) | Adds support for dot notation and type guessing to ConfigParser. | None except additional dependency. |
Json | Built-in (Standard Library) | Highly structured. Loads to a dictionary. Wide developer familiarity. | JSON is verbose to edit. No support for comments. |
Tomllib | Built-in (Standard Library) | Extremely easy to use. Format designed for configuration. Supports comments. Typed values. Loads as a dictionary. This is a top pick (see the section below). | Python 3.11 and later only. |
Toml | Installable with pip or conda | Excellent drop-in "replacement" for tomllib for Python versions before 3.11. See tomllib for details. This is a top pick (see the section below). | None except additional dependency. |
Pyaml | Installable with pip or conda | Loads to a plain dictionary, values are typed (not plain strings). | Yaml is easy to break during editing. |