Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Configuration Files

00:00 Use Configuration Files in Your Projects. You have a project with some settings that you want to extract into a configuration file. Recall that there are several ways that a configuration can improve your code base.

00:13 In names, values, and concepts, it provides more visibility for specific values, and it makes those values easier to change. A configuration file can help you get an overview of your source code and add flexibility to how your users interact with your application.

00:29 You know how to read a TOML-based configuration file, but how do you use it in a project? In particular, how do you make sure that the configuration file is only parsed once and how do you access the configuration from different modules?

00:43 It turns out that Python’s import system already supports these features out of the box. When you import a module, it’s cached for later use. In other words, if you wrap your configuration in a module, you know that the configuration will only be read one time, even if you import that module from several places. It’s time for an example. Recall the tic-tac-toe.toml configuration file from earlier on.

01:08 Create a directory named config/ and save tic-tac-toe.toml inside that directory. Additionally, create an empty file named __init__ .py inside of the config/ directory.

01:21 The directory structure should look as seen on screen.

01:26 Files named __init__.py play a special role in Python. They mark the containing directory as a package. Additionally, names defined inside __init__ .py are exposed through the package, and you’ll see what this means shortly.

01:42 Now, add some code to __init__ .py to read the configuration file.

01:51 You use pathlib and the special __file__ variable to set up path, the full path to the TOML file. In practice, this specifies that the TOML file is stored in the same directory as __init__ .py. You read the TOML file using load as earlier and store the TOML data to the name tic_tac_toe.

02:14 To try out this package, start a REPL session from the parent directory of config/. You can check the path of the configuration and access the configuration itself. To read particular values, you can use regular item access.

02:44 You can now integrate a configuration into your existing projects by copying the config/ directory into your project and replacing the tic-tac-toe configuration with your own settings. Inside your code files, you may want to alias the configuration import to make it more convenient to access the settings.

03:05 Here, you name the configuration CFG during import, which makes it both efficient and readable to access configuration settings. This gives you a quick and reliable way of working with a configuration in your own projects.

03:19 But if you’re going to become totally fluent in using TOML in your own Python applications, then there’s an important skill currently missing, and that’s the ability to write TOML from Python.

03:29 That’s what you’ll be doing in the next section of the course.

Become a Member to join the conversation.