How to set environment variables in Python?

Posted on

Solving problem is about exposing yourself to as many situations as possible like How to set environment variables in Python? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How to set environment variables in Python?, which can be followed any time. Take easy to follow this discuss.

How to set environment variables in Python?

I need to set some environment variables in the Python script and I want all the other scripts that are called from Python to see the environment variables’ set.

If I do,

os.environ["DEBUSSY"] = 1

it complains saying that 1 has to be a string.

I also want to know how to read the environment variables in Python (in the latter part of the script) once I set it.

Answer #1:

Environment variables must be strings, so use

os.environ["DEBUSSY"] = "1"

to set the variable DEBUSSY to the string 1.

To access this variable later, simply use:


Child processes automatically inherit the environment variables of the parent process — no special action on your part is required.

Answered By: Sven Marnach

Answer #2:

You may need to consider some further aspects for code robustness;

when you’re storing an integer-valued variable as an environment variable, try

os.environ['DEBUSSY'] = str(myintvariable)

then for retrieval, consider that to avoid errors, you should try

os.environ.get('DEBUSSY', 'Not Set')

possibly substitute ‘-1’ for ‘Not Set’

so, to put that all together

myintvariable = 1
os.environ['DEBUSSY'] = str(myintvariable)
strauss = int(os.environ.get('STRAUSS', '-1'))
# NB KeyError <=> strauss = os.environ['STRAUSS']
debussy = int(os.environ.get('DEBUSSY', '-1'))
print "%s %u, %s %u" % ('Strauss', strauss, 'Debussy', debussy)
Answered By: Mark

Answer #3:

os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set operations mentioned in the other answers, we can also simply check if a key exists. The keys and values should be stored as strings.

Python 3

For python 3, dictionaries use the in keyword instead of has_key

>>> import os
>>> 'HOME' in os.environ  # Check an existing env. variable

Python 2

>>> import os
>>> os.environ.has_key('HOME')  # Check an existing env. variable
>>> os.environ.has_key('FOO')   # Check for a non existing variable
>>> os.environ['FOO'] = '1'     # Set a new env. variable (String value)
>>> os.environ.has_key('FOO')
>>> os.environ.get('FOO')       # Retrieve the value

There is one important thing to note about using os.environ:

Although child processes inherit the environment from the parent process, I had run into an issue recently and figured out, if you have other scripts updating the environment while your python script is running, calling os.environ again will not reflect the latest values.

Excerpt from the docs:

This mapping is captured the first time the os module is imported,
typically during Python startup as part of processing Changes
to the environment made after this time are not reflected in
os.environ, except for changes made by modifying os.environ directly. which stores all the environment variables, is a dict object, which contains all the environment values:

>>> type(  # changed to _data since v3.2 (refer comment below)
<type 'dict'>
Answered By: sisanared

Answer #4:

if i do os.environ[“DEBUSSY”] = 1, it
complains saying that 1 has to be

Then do

os.environ["DEBUSSY"] = "1"

I also want to know how to read the
environment variables in python(in the
later part of the script) once i set

Just use os.environ["DEBUSSY"], as in

some_value = os.environ["DEBUSSY"]
Answered By: Jim Brissom

Answer #5:

I have been trying to add environment variables. My goal was to store some user information to system variables such that I can use those variables for future solutions, as an alternative to config files.
However, the method described in the code below did not help me at all.

import os
os.environ["variable_1"] = "value_1"
os.environ["variable_2"] = "value_2"
# To Verify above code

This simple code block works well, however, these variables exist inside the respective processes such that you will not find them in the environment variables tab of windows system settings. Pretty much above code did not serve my purpose. This problem is discussed here: variable save problem

os.environ.putenv(key, value)

Another unsuccessful attempt. So, finally, I managed to save variable successfully inside the window environment register by mimicking the windows shell commands wrapped inside the system class of os package. The following code describes this successful attempt.

os.system("SETX {0} {1} /M".format(key, value))

I hope this will be helpful for some of you.

Answered By: Sourabh Desai

Answer #6:

to Set Variable:

item Assignment method using key:

import os
os.environ['DEBUSSY'] = '1'  #Environ Variable must be string not Int

to get or to check whether its existed or not,

since os.environ is an instance you can try object way.

Method 1:

os.environ.get('DEBUSSY') # this is error free method if not will return None by default

will get '1' as return value

Method 2:

os.environ['DEBUSSY'] # will throw an key error if not found!

Method 3:

'DEBUSSY' in os.environ  # will return Boolean True/False

Method 4:

os.environ.has_key('DEBUSSY') #last 2 methods are Boolean Return so can use for conditional statements

Answer #7:

You should assign string value to environment variable.

os.environ["DEBUSSY"] = "1"

If you want to read or print the environment variable just use

print os.environ["DEBUSSY"]

This changes will be effective only for the current process where it was assigned, it will no change the value permanently. The child processes will automatically inherit the environment of the parent process.

enter image description here

Answered By: RejeeshChandran

Answer #8:

What about os.environ["DEBUSSY"] = '1'? Environment variables are always strings.

Answered By: ThiefMaster

Leave a Reply

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