How to Get and Set Environment Variable in Python?
Before checking how to get and set environment variables in Python, let’s see- what is an environment variable, uses, and how to check all the environment variables in Windows and Linux.
If you are already aware of it, you can directly jump to Python code for getting and setting environment variables.
What is Environment Variable?
The environment variable is the object which contains the name-value pair data. And there is a set of environment variables.
These environment variables are set at the Operating System level.
How to find all the Environment Variables?
- Open the command prompt and
- Run command set
This will list out all the environment variables.
- Open Linux terminal
- Run command env
This gives a list of all the envelopment variables set on your Linux system.
What is the Use of Environment Variables?
Usually, environment variables are used as configuration variables for other running applications. So, by defining and setting the environment variable, you can change the behavior of your application.
You can create new environment variable and set/get the value of the existing environment variable.
Sometimes, the values of the environment variables are updated by the system applications automatically.
One such example is the “PWD” environment variable. It stands for “Print Working Directory”.
“PWD” variable stores the current directory path. Every time when you change the current directory path in the terminal, this variable value changes.
Get and Set Environment Variable in Python
You can use the “OS” module to get, set, and extract the environment variable in Python. Let’s see the getting and setting environment variable one-by-one.
Get Environment Variable
getenv() method which is defined in the
env_variable_name is any valid variable name.
In this coding example, let’s print the home directory path. Home directory path is stored in the “HOME” variable.
import os home_path = os.getenv("HOME") print(home_path)
You might get a different output. You can verify by matching the output with the value of that environment variable.
If the given environment variable is not present,
getenv() method will return None type value.
Set Environment Variable
You can change the value of the environment variable through your Python code.
environ() method which is defined in the
environ[<env_variable_name>] = <value>
You can set any valid value like string, bool or number.
Let’s change the “HOME” directory path.
import os os.environ("HOME") = "/home/user2"
This will change the home directory path.
Before changing the environment variable, check what all other applications are using that environment variable. Make sure, changing the value of the environment variable is not impacting other applications.
Otherwise, if you are changing the environment variable as part of coding practice, export your own environment variable and then get and set the value of that environment variable.
Setting Environment Variable from File
While working on one of the Python projects, I come across setting multiple environment variables before executing the Project file.
For this, you can write all the environment variables in the text file. Read the file, loop over all the environment variables and set them one-by-one
I found this “OS” module very useful while working on the Python project. Apart from dealing with the environment variables, you can do a lot of things with this module, like…
- Clearing Python interpreter console
- Checking if given file/directory exists in Python
- Getting the file size in Python
Any query? Let me know in the comment.