In Python, you can access the environment variables using os.environ
module.
- What is an environment variable?
- How do I access all environment variables in Linux?
- How can I access an environment variable in Python?
- os.environ[] vs os.environ.get()
- How can I list all environment variables?
- How can I set environment variables?
- Store environment variables in .env files
- How to handle sensitive information
- How to name environment variables
- Conclusion
What is an environment variable?
An environment variable is a variable whose value is set externally, outside the program. The common place is to add it in the operating system, with a way to read it or change it. An environment variable consists of a key-value pair.
If you are using Linux and want to set an environment variable ACCESS_TOKEN
, you can do it on the command line like this:
$ export ACCESS_TOKEN='abcdefghij1234567890'
and print it from the command line:
$ echo $ACCESS_TOKEN
abcdefghij1234567890
This environment variable ACCESS_TOKEN
can be accessed by external programs as well.
How do I access all environment variables in Linux?
Run this command on the command line to show all environment variables in Linux.
export
The output of this command will be all environment variables currently in use.
How can I access an environment variable in Python?
We will show you how to read ACCESS_TOKEN
and other environment variables from Python.
To list environment variable ACCESS_TOKEN
, you can use either of these statements:
print(os.environ['ACCESS_TOKEN'])
print(os.environ.get('ACCESS_TOKEN'))
Both these statements can be used to retrieve environment variables. There is a difference between those two statements, though.
os.environ[] vs os.environ.get()
If you are trying to find the value of the environment variable NON_EXISTING_ACCESS_TOKEN
and it does not exist, your program will throw a KeyError error and stop execution.
print(os.environ['NON_EXISTING_ACCESS_TOKEN'])
OUTPUT:
KeyError: 'NON_EXISTING_ACCESS_TOKEN'
If you want your program to continue if env variable NON_EXISTING_ACCESS_TOKEN
does not exist, you can use os.environ.get()
.
print(os.environ.get('NON_EXISTING_ACCESS_TOKEN'))
OUTPUT:
None
This is easier to handle.
How can I list all environment variables?
You can list all environment variables using this Python code:
import os
print(os.environ)
This prints all the environment variables as a hashmap with key-value pairs, in no organized format.
To print the variables in a properly formatted way:
import os
for key, value in os.environ.items():
print(f'{key}: {value}')
The output will be all the environment variable names and their values printed neatly in a KEY: VALUE
format.
How can I set environment variables?
Let us create a new environment variable NEW_ACCESS_TOKEN
and assign it value of 'abc'.
os.environ['NEW_ACCESS_TOKEN'] = 'abc'
Store environment variables in .env files
If your program is going to use or set multiple environment variables, it is good to store them in a file. The usual convention is to create a .env
file and store them there.
Create a file called .env
and add these lines to it:
ACCESS_TOKEN=abc123
USERNAME=duke
PASSWORD=nukem
If you want to read this file and use the key-value pairs as environment variables, use the dotenv
module. You will have to install it first.
pip install python-dotenv
After installation, create a Python program to read it. Our code will read it and set the environment variables stored in .env
.
from dotenv import load_dotenv
import os
load_dotenv()
print(os.environ['ACCESS_TOKEN'])
print(os.environ['USERNAME'])
print(os.environ['PASSWORD'])
The statement load_dotenv()
loads everything in .env
and sets the environment variable for each key to its value. It then prints each value as shown in the code.
How to handle sensitive information
If you store sensitive data like API secrets, passwords and access tokens as environment variables instead of hardcoding them, that improves security of your program. Also, if you are using an external .env
or similar environment variable file, remember to chmod it appropriately, possibly with 400, so that unauthorized people will neither be able to read not write to it.
How to name environment variables
It is good to use uppercase letters and underscores. Also, if possible, be specific on certain details to reduce ambiguity.
For example, if you want to create an environment variable that contains the frequency of run every 7 hours, instead of this:
FREQUENCY=7
do this:
FREQUENCY_HOURS=7
That change will be more understandable to anyone reading the code. Of course, your situation may be different, so act accordingly and have someone peer-review your code before pushing it to production.
Do use a .env
for location development with key-value pairs, one on each line. That is more manageable than storing it in .bashrc
or .bash_profile
files.
Conclusion
We have reviewed the usage of os.environ
and how to retrieve and set environment variables. Your use cases will vary, so do everything with proper approval, and peer review your syntax and project structure.
If you have any questions about handling environment variables in Python, feel free to contact me.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.