How to Set Environment Variables in Python

Environment variables are a set of dynamic named values that can affect the way running processes behave on a computer.

They are used to store configuration information, sensitive data, and other information that is required by applications to run smoothly.

In this tutorial, we will explore how to set environment variables in Python.


Introduction to Environment Variables

Environment variables are a global system variable that is stored in the operating system and can be accessed by any process running on the computer.

They are used to store data that is required by an application to run.

For example, the PATH environment variable on a computer running Microsoft Windows specifies the location of the system’s executables, while on a computer running Unix, it specifies the directories where the shell should look for commands.

Setting Environment Variables in Python

In Python, environment variables can be set using the os module.

The os module provides a simple way to interact with the operating system and perform various operations, including setting and reading environment variables.

Here are the steps to set environment variables in Python:

1. Import the os module: To use the os module, you need to import it in your Python script using the following code:

import os

2. Set the environment variable: You can set the environment variable using the os.environ dictionary. The syntax for setting an environment variable is:

os.environ['VARIABLE_NAME'] = 'value'

Here, VARIABLE_NAME is the name of the environment variable, and value is the value that you want to set for that variable.

Example: To set the PATH environment variable in Python, use the following code:

import os

os.environ['PATH'] = '/usr/bin'

Get the value of the environment variable: To retrieve the value of an environment variable, you can use the following code:

import os

path = os.environ.get('PATH')
print(path)

In this example, the value of the PATH environment variable is stored in the path variable.

Note: The os.environ.get() method returns None if the environment variable does not exist.

Accessing Environment Variables in a Different Process

When you set an environment variable in a Python script, it is only available in that process and its child processes.

If you want to access the environment variable in a different process, you need to set it in the operating system.

Setting environment variables in the operating system depends on the operating system you are using. Here are the steps to set environment variables in different operating systems:

Windows:

  • Open the Start menu and search for Environment Variables.
  • Click on Edit the system environment variables.
  • Click on the Environment Variables button.
  • In the User variables section, click on New and enter the variable name and value.
  • Click on OK to save the changes.

macOS and Linux:

  • Open a terminal window.
  • Run the following command to set the environment variable:
export VARIABLE_NAME=

Here, VARIABLE_NAME is the name of the environment variable, and value is the value that you want to set for that variable.

Note: To persist the environment variable in the operating system, you need to add the export command to your shell profile file, such as .bash_profile or .bashrc file on Linux, or .bash_profile on macOS.


Conclusion

In this article, we explored how to set and access environment variables in Python.

We saw how to set environment variables using the os module and how to retrieve their values.

We also saw how to set environment variables in different operating systems to make them available in other processes.

Environment variables are a powerful tool for storing configuration information and sensitive data, and they play a crucial role in many applications.

With this article, you now have a solid understanding of how to set and access environment variables in Python.