Python Read Environment Variable

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python environment variables are utilized to change the system configuration, and the output of many python applications depends on the values of the environment variables. Even environment variables help you to write smoother workflows. They are accessed via the os module in Python.

Python environment variables make code more secure by masking sensitive data needed to assign environment variables, such as API tokens.

What is Environment Variable in Python

Python environment variables exist outside your code and as part of your current user's or system's configuration. Environment variables characterize key-value pairs of data where the key is the environment variable name, and the value is the environment variable value that can be accessed through your code.

Python Environment Variables help you write more streamlined and secure code because the important data can be saved inside the environment variable and the script file shared without worrying about exposing sensitive information.

Python read environment Variables can be read and set with the help of functions provided by OS modules which help to interact with the operating system.

Reading Environment Variable in Python

Reading Python environment variables requires the import of the os module. Os module provides an environ property to get or read the dictionary of all the python environment variables.

Suppose you want to read all the python environment variables, which can be possible with os.environ.

Here's how you can do it:-

The above code illustrates that we're importing the os module with the help of the import statement and then printing the environment variables with the use of the print() function and os.environ method to get the Python read environment Variable.

Output:-

The below image shows a dictionary of python variable environments as key-value pairs.

output-os.environ-as-dictionary

The above output of os.environ as a dictionary could be more precise. Let's make it better visually by looping through the dictionary and printing the keys and values.

Here's how you can do it:-

We are getting the python environment variable dictionary in the above code and looping through it to print the key-value pairs.

Output:-

output-pyton-environment-dictionary-and-looping-through-it

More Examples for Understanding

In the above, We looked at dictionary examples of all Python environment variables, but you can also read the value of a specific environment variable value.

Here's how you can do it:-

In the above, we get the PATH environment variable's value, store it inside the path variable, and print it to display the value.

Output:-

path-environment-variable-stored-inside-path-variable

FAQs

Q. What is the PATH Variable Used For?

A. PATH is an environment variable in Windows that tells the interpreter where to find the module files imported into the program.

Q. How do I Add an Environment Variable?

A. You can add environmental variables using the os.environ method using the os.environ["key"] = "value" syntax.

Q. Is os.environ the Only Method to Change the Environment Variable?

A. No, You can also change the environment variables manually by changing your system python variables configuration.

Conclusion

  • The value pairs of the environment variables can be set or read by utilizing the os.environ of the os module.
  • Environment variables provide the capability to run your code securely.
  • Environment variables allow you to streamline your code without repeating and changing it. Likewise, they prevent you from sharing secure items such as API Keys.