access environment variable in python

How to Access Environment Variables in Python

Environment variables are file/folder locations or other values that can be used from any python script on your system. Sometimes you may need to get or set environment variables in Python. In this article, we will learn how to access environment variables in Python.


How to Access Environment Variables in Python

Here are the steps to access environment variables in Python.


1. Access Environment Variables

You can access environment variables using os system module. Here are the commands to access environment variable HOME and print its value.

import os
print(os.environ['HOME'])

Basically, os.environ returns a key-value dictionary consisting of all environment variables as keys along with their values.

There are multiple environment variables in every system, in addition to HOME, and you can access them by specifying them in your command as shown below. Replace KEY below with your environment variable’s name.

# using get will return `None` if a key is not present rather than raise a `KeyError`
print(os.environ.get('KEY'))

The above command will return None if key is not present, instead of raising a KeyError.

If you don’t want None to be returned, you can specify the default value to be returned, using getenv() function. Replace default_value with the default value you want to be returned.

# os.getenv is equivalent, and can also give a default value instead of `None`
print(os.getenv('KEY', default_value))
OR
print(os.environ.get('KEY', default_value))

Here is an example to return default value for HOME environment variable.

print(os.getenv('HOME', '/home/username/'))


2. Check if Environment Variable Exists

If you want to check if environment variable exists you can use the ‘in’ operator for this purpose, just as you check if a key exists in Python dictionary. Here is an example to check if HOME variable exists in environment variable.

import os 

if 'HOME' in os.environ:
    print(os.environ['HOME'])


3. Set Environment

In this short article, we have learnt how to access environment variables in python. Now we will look at how to set their values. You can set their values just as you set a key-value pair in python dictionary.

Here is a simple example to set HOME environment variable.

import os
os.environ['HOME']='/home/ubuntu/python'
print(os.environ['HOME'])

Please note you need to provide a string value when you set an environment variable. If you provide a number you will get an error. The following code will throw an error.

import os
os.environ['HOME']=123

In this short article, we have learnt how to access environment variables, get and set their values.

Also read:

How to Password Protect PDF File in Python
How to Read Inputs as Numbers in Python
How to Split List into Even Sized Chunks
How to Fix Temporary Failure in Name Resolution in Linux
How to Add Route in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *