3319

How can I get the value of an environment variable in Python?

0

17 Answers 17

4663

Environment variables are accessed through os.environ:

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

To see a list of all environment variables:

print(os.environ)

If a key is not present, attempting to access it will raise a KeyError. To avoid this:

# Returns `None` if the key doesn't exist
print(os.environ.get('KEY_THAT_MIGHT_EXIST'))

# Returns `default_value` if the key doesn't exist
print(os.environ.get('KEY_THAT_MIGHT_EXIST', default_value))

# Returns `default_value` if the key doesn't exist
print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))
12
  • 14
    os.environ is a dictionary. Trying to access a key not present in the dictionary will throw a KeyError. The get method simply returns None when the key does not exists. Do you have PYTHONPATH set? Can you try with a variable such as PATH, that is guaranteed to exist? Does it return a meaningful value?
    – Rod
    Commented Feb 5, 2011 at 19:21
  • I haven’t set it (PYTHONPATH) before; what I am doing just go with command prompt and type CMD anywhere (since python.exe is in my shell PATH). If I try to access Window ENVIRONMENT variable, it gives mapped value but the problem with Python ENVIRONMENT variable like; PYTHONPATH and PYTHONHOME.
    – Amit Yadav
    Commented Feb 7, 2011 at 8:02
  • 3
    PYTHONPATH is used to add new search path to Python (sys.path) from outside Python. Have a look at docs.python.org/using/cmdline.html#environment-variables
    – Rod
    Commented Feb 7, 2011 at 14:41
  • Is there any way to keep this variable persistent ?? When I run python later on the environment variable is gone and gives a raise KeyError(key) instead
    – mrid
    Commented Jan 29, 2017 at 7:29
  • 45
    .get() can also be given a default. Commented Sep 21, 2018 at 15:25
366

To check if the key exists (returns True or False)

'HOME' in os.environ

You can also use get() when printing the key; useful if you want to use a default.

print(os.environ.get('HOME', '/home/username/'))

where /home/username/ is the default

3
  • 8
    Which is better, "HOME" in os.environ or os.environ.get('HOME')?
    – endolith
    Commented Feb 3, 2017 at 16:11
  • 21
    @endolith They do different things. The first returns True or False, while the second returns a value, possibly None.
    – Trenton
    Commented Feb 13, 2018 at 22:38
  • 7
    @endolith, the correct question woud be "HOME" in os.environ vs os.environ.get('HOME') is None. As you can see first is far more readable & comfortable to work with. Commented Oct 16, 2019 at 13:43
102

Actually it can be done this way:

import os

for key, value in os.environ.items():
    print(f'{key}: {value}')

Or simply:

for key, value in os.environ.items():
    print('{}: {}'.format(key, value))

or:

for i, j in os.environ.items():
    print(i, j)

For viewing the value in the parameter:

print(os.environ['HOME'])

Or:

print(os.environ.get('HOME'))

To set the value:

os.environ['HOME'] = '/new/value'
4
  • 17
    No, this answer really doesn't add anything on top of the existing answers
    – Bart
    Commented May 2, 2018 at 10:06
  • 6
    This should be removed, it is a duplicate of other answers. str.format is just a fancy addition.
    – moltarze
    Commented Apr 21, 2019 at 16:46
  • 4
    The first answer with readable output for the entire env, thanks. To view the env in the PyCharm debugger, I evaluate {k: v for k,v in sorted(os.environ.items())}
    – Noumenon
    Commented Aug 4, 2021 at 22:55
  • 4
    it adds how to set the value
    – M.C.
    Commented Nov 26, 2021 at 10:21
81

Here's how to check if $FOO is set:

try:  
   os.environ["FOO"]
except KeyError: 
   print "Please set the environment variable FOO"
   sys.exit(1)
4
  • 6
    Try can be faster. The case of env vars is likely best for 'try': stackoverflow.com/a/1835844/187769 Commented Feb 5, 2017 at 16:49
  • 31
    @RandomInsano faster =/= better. This code looks less readable than an "if 'FOO' not in os.environ: ..." block
    – Dangercrow
    Commented Oct 13, 2017 at 13:27
  • If you just setup the variable you need to close the Terminal before you can test it.
    – Shane S
    Commented Jun 16, 2022 at 5:21
  • Could the environment variable change (from another thread or from an outside source) between the if check and the access? Because these TOCTTOU (time of check to time of use) problems are avoided with the try except construct!
    – xuiqzy
    Commented Nov 30, 2023 at 13:28
67

You can access the environment variables using

import os
print os.environ

Try to see the content of the PYTHONPATH or PYTHONHOME environment variables. Maybe this will be helpful for your second question.

44

As for the environment variables:

import os
print os.environ["HOME"]
36

Import the os module:

import os

To get an environment variable:

os.environ.get('Env_var')

To set an environment variable:

# Set environment variables
os.environ['Env_var'] = 'Some Value'
34
import os
for a in os.environ:
    print('Var: ', a, 'Value: ', os.getenv(a))
print("all done")

That will print all of the environment variables along with their values.

26

If you are planning to use the code in a production web application code, using any web framework like Django and Flask, use projects like envparse. Using it, you can read the value as your defined type.

from envparse import env
# will read WHITE_LIST=hello,world,hi to white_list = ["hello", "world", "hi"]
white_list = env.list("WHITE_LIST", default=[])
# Perfect for reading boolean
DEBUG = env.bool("DEBUG", default=False)

NOTE: kennethreitz's autoenv is a recommended tool for making project-specific environment variables. For those who are using autoenv, please note to keep the .env file private (inaccessible to public).

1
  • 2
    envparse is used by about 4,000 people and has not been maintained since 2015, vs. dotenv, which is used by 240,000 people. The people have spoken. Commented Jun 11, 2022 at 17:06
17

There are also a number of great libraries. Envs, for example, will allow you to parse objects out of your environment variables, which is rad. For example:

from envs import env
env('SECRET_KEY') # 'your_secret_key_here'
env('SERVER_NAMES',var_type='list') #['your', 'list', 'here']
1
  • 3
    What does "rad" mean in "which is rad"? rad - "1. (slang) Clipping of radical; excellent" Commented Jul 25, 2021 at 21:06
13

Edited - October 2021

Following @Peter's comment, here's how you can test it:

main.py

#!/usr/bin/env python


from os import environ

# Initialize variables
num_of_vars = 50
for i in range(1, num_of_vars):
    environ[f"_BENCHMARK_{i}"] = f"BENCHMARK VALUE {i}"  

def stopwatch(repeat=1, autorun=True):
    """
    Source: https://stackoverflow.com/a/68660080/5285732
    stopwatch decorator to calculate the total time of a function
    """
    import timeit
    import functools
    
    def outer_func(func):
        @functools.wraps(func)
        def time_func(*args, **kwargs):
            t1 = timeit.default_timer()
            for _ in range(repeat):
                r = func(*args, **kwargs)
            t2 = timeit.default_timer()
            print(f"Function={func.__name__}, Time={t2 - t1}")
            return r
        
        if autorun:
            try:
                time_func()
            except TypeError:
                raise Exception(f"{time_func.__name__}: autorun only works with no parameters, you may want to use @stopwatch(autorun=False)") from None
        
        return time_func
    
    if callable(repeat):
        func = repeat
        repeat = 1
        return outer_func(func)
    
    return outer_func

@stopwatch(repeat=10000)
def using_environ():
    for item in environ:
        pass

@stopwatch
def using_dict(repeat=10000):
    env_vars_dict = dict(environ)
    for item in env_vars_dict:
        pass
python "main.py"

# Output
Function=using_environ, Time=0.216224731
Function=using_dict, Time=0.00014206099999999888

If this is true ... It's 1500x faster to use a dict() instead of accessing environ directly.


A performance-driven approach - calling environ is expensive, so it's better to call it once and save it to a dictionary. Full example:

from os import environ


# Slower
print(environ["USER"], environ["NAME"])

# Faster
env_dict = dict(environ)
print(env_dict["USER"], env_dict["NAME"])

P.S- if you worry about exposing private environment variables, then sanitize env_dict after the assignment.

2
  • 2
    What do you mean sanitize? Like remove entries with sensitive data so maybe you don't accidentally throw api keys or secrets in app logs? Commented Dec 13, 2022 at 0:19
  • @ferreiradev yup, exactly that
    – Meir Gabay
    Commented Dec 13, 2022 at 5:55
11

You can also try this:

First, install python-decouple

pip install python-decouple

Import it in your file

from decouple import config

Then get the environment variable

SECRET_KEY=config('SECRET_KEY')

Read more about the Python library here.

10

You can use python-dotenv module to access environment variables

Install the module using:

pip install python-dotenv

After that, create a .env file that has the following entry:

BASE_URL = "my_base_url"

Then import the module into your Python file

import os
from dotenv import load_dotenv

# Load the environment variables
load_dotenv()

# Access the environment variable
print(os.getenv("BASE_URL"))
2
  • 9
    why would you install a third party library for something so simple?
    – Or Yaacov
    Commented Nov 16, 2023 at 15:45
  • This reads from a file, not from the environment. Commented Jun 7 at 15:00
7

For Django, see Django-environ.

$ pip install django-environ

import environ

env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()

# False if not in os.environ
DEBUG = env('DEBUG')

# Raises Django's ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')
1
  • 3
    An explanation would be in order. What is the context - in what context is the code executed? On a server with Django? Locally for testing it out? Somewhere else? What is the idea? What is the code supposed to accomplish? Commented Jul 25, 2021 at 21:11
4

You should first import os using

import os

and then actually print the environment variable value

print(os.environ['yourvariable'])

of course, replace yourvariable as the variable you want to access.

3

The tricky part of using nested for-loops in one-liners is that you have to use list comprehension. So in order to print all your environment variables, without having to import a foreign library, you can use:

python -c "import os;L=[f'{k}={v}' for k,v in os.environ.items()]; print('\n'.join(L))"
2

With os:

import os

# get the value of the environment variable HOME
os.getenv('HOME')
os.environ['HOME']

# show all environment variables (like `set` in bash)
os.environ

# set environment variable MYVAR (note: it has to be a string)
os.environ["MYVAR"] = "x"

# unset variable MYVAR
del os.environ["MYVAR"]

For interactive work with IPython/Jupyter notebooks the magic %env is also practical:

%env
%env HOME
%env MYVAR = "x"
# numbers are also allowed 
%env MYVAR = 3

Not the answer you're looking for? Browse other questions tagged or ask your own question.