How to Use Environment Variables in Python?
These are the different ways of accessing environment variables:
- Using os.environ()
- Access All Environment Variables
- Access Single Environment Variable
- Get value of the Environment variable
- Using os.getenv()
- Using python-dotenv Package
Access All Environment Variables Using os.environ()
Here, we are accessing all the environment variables that are present in the environment.
Python3
# import os module import os # display all environment variable print (os.environ) |
Output
Access Single Environment Variable Using os.environ()
Here, we are extracting single environment variable of COMPUTERNAME from the above list. If we try to access an environment variable that is not available we will get KeyError.
Python3
# import os module import os # access environment variable print (os.environ[ 'COMPUTERNAME' ]) |
Output
DESKTOP-M2ASD91
Get value of the environment variable key using os.environ
Here, we are extracting single environment variable key of USERPROFILE path from the environ list. This will return None if the given key is not found.
Python3
# import os module import os # access environment variable using the key print (os.environ.get( 'USERPROFILE' )) |
Output
C:\Users\suraj
Return default value if the key doesn’t exist
Python3
# import os module import os # return default value if no # key/environment variable if found print (os.environ.get( 'DATABASE_NAME' , 'example.database.net' )) |
Output
example.database.net/
Access Environment Variables Using os.getenv()
In this example, the code uses the os.getenv()
function to retrieve the value of the ‘PATH’ environment variable and assigns it to the variable home_dir
. The script then prints the obtained ‘PATH’ value.
Python3
import os home_dir = os.getenv( 'PATH' ) print (home_dir) |
Output
Use Environment Variables Using Python dotenv Package
Firstly install python-dotenv
package by using the following command:
pip install python-dotenv
Create a file named .env
in the root of your project and add your environment variables with the format KEY=VALUE
. For example:
DATABASE_URL=mydatabaseurl
API_KEY=yourapikey
Use the dotenv
module to load the variables from the .env
file. Then, you can access them like regular Python variables. In this code, load_dotenv()
is used to load environment variables from a .env
file. The script then retrieves the values of “DATABASE_URL” and “API_KEY” using os.getenv()
and prints them for use in the script.
Python3
from dotenv import load_dotenv import os # Load environment variables from the .env file load_dotenv() # Access environment variables database_url = os.getenv( "DATABASE_URL" ) api_key = os.getenv( "API_KEY" ) # Now you can use these variables in your script print (f "Database URL: {database_url}" ) print (f "API Key: {api_key}" ) |
Output
Database URL: mydatabaseurl
API Key: yourapikey
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python’s we can use the OS module which provides a property called environ that contains environment variables in key-value pairs. In this article, we will see how to use environment variables in Python.
Contact Us