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

Configuring Constants

00:00 Configuration Files and Environment Variables Let’s say you want to take things a step further when it comes to externalizing the constants of a given project.

00:10 You may need to keep all your constants out of your project source code. For instance, if you are going to make the project code public in a code repository such as GitHub. One way to do this is to use an external configuration file. On screen you can see how to move the constants to a configuration file.

00:29 This file uses the .ini file format. You can read this type of file using the configparser module from the standard library.

00:39 Now go back to calculations.py and update it to use the new configuration file.

00:56 Here, the code first reads the configuration file and stores the resulting configparser object in a global variable constants. You could also name this variable as a constant in uppercase and use it globally as a constant.

01:10 Then you update your calculations to read the constants from the configuration object itself. Note that configparser objects store the configuration parameters as strings, so you need to use the built-in float() function to convert them into numbers.

01:49 This strategy may be beneficial if you are creating a graphical user interface app and need to set some parameters to define the shape and size of the app’s windows when loading and showing the GUI.

02:01 Another helpful strategy to handle your constants is to define them as a system variable if you are on Windows or environment variables if you are on macOS or Linux. This approach is commonly used to configure deployment in disparate environments.

02:15 You can also use environment variables for constants that imply security risks and should not be directly communicated to the source code. Examples of these types of constants include authentication credentials, API access tokens, and so on.

02:31 You should be careful when using environmental variables for sensitive information because they may be accidentally exposed in logs or to child processes.

02:39 All cloud providers offer some kind of secrets management that is more secure. To use this strategy, you must first export your constants as environment or system variables in your operating system.

02:51 There are at least two ways to do this. You can manually export the constants or you can add them to your shell’s configuration file.

03:01 Manually exporting the constants is quick and practical. You can use it to run some fast tests on your code. Let’s say you need to export an API token as a system or environment variable.

03:13 In that case, you just need to run a single command. Here you can see it running on Windows,

03:25 and here you can see the line you would need for Linux or macOS.

03:34 The main drawback of this technique is that your constants will be accessible only from the command line session in which you have defined them. A much better approach is to make your operating system load the constants whenever you fire up a command-line window. If you are on Windows, then check out the configuring environment variable section in this Real Python article to learn how to create system variables.

03:57 You can follow the instructions in this guide to add the API token system variable. If you are on Linux or macOS, then you can go to your home folder and open the shell’s configuration file.

04:10 Once you’ve opened the file, you can add the export to it. On screen, you can see the file being edited on Ubuntu, which uses the Bash shell. You edit the file in nano with the command seen on screen.

04:40 Once you have added the export to the file, you can save it with control and o, hit Enter to confirm the file name, and then exit nano with control and x, returning you back to the terminal.

04:53 And here’s the same on macOS, which uses the Z shell as the default since Catalina, so it’s the same edit, but to a different file.

05:16 Linux and macOS automatically load the corresponding shell configuration file whenever you start a terminal or command-line window. This way you ensure that the API_TOKEN variable will always be available on your system.

05:30 Once you define the required environment variables for your Python constant, then you need to load them into your code. To do this, you can use the os.environ dictionary from Python’s os module.

05:41 The keys and values of os.environ are strings representing the environment variables and their values respectively. The API_TOKEN constant is now present in the os.environ dictionary.

05:53 Therefore, you can read it with the two lines of code seen on screen.

06:02 Using environment variables to store constants and the os.environ dictionary to read them into your code is an effective way of configuring constants that depend on the environment your application is deployed in.

06:13 It’s particularly useful when working in the cloud, so keep this technique in your Python toolkit.

06:20 In the next section of the course, you’ll deepen your understanding by taking a look at some other types of constants that are available to you in Python.

Become a Member to join the conversation.