Problem Formulation: Say, you set an environment variable and you want to access it in your Python script. How to get the value of the environment variable?
Background: Environment variables are external to the Python environment. They describe the operating system environment we are using.
The applications installed use this information to complete specific tasks such as storing temporary files, accessing the user profile, or locating particular files and data.
Some of these variables contain environment configuration information, such as the number of processors installed that other applications use to improve their performance. Usually, the Windows Registry is used to store the configuration information, and applications access the registry to retrieve it.
Furthermore, using environment variables minimizes hardcoding, and thus, applications are more portable and easier to move to another server or operating system.
Python accesses the environment variables via the os
mapping class module and the dictionary returned by the os.environ
mapping object.
The mapping occurs when the os
module loads. Therefore any changes to the environment variables after the os
module loads are not included.
How to Get and Set Environment Variables in Python?
The os.environ
provides a dictionary data structure of all the environment variables set for the specific operating system. Therefore, one can use all Python dictionary methods to manage the dictionary and retrieve the required information.
Once the os
module is loaded, creating a new environment variable and accessing its value is simple.
import os os.environ['ENV_VAR'] = 'username' # SET environment variable ENV_VAR
However, If the environment variable exists, we will overwrite its value, and perhaps the application using it will malfunction.
To ensure we don’t overwrite existing variable we can use an if statement to check for the variable existence:
If os.environ['ENV_VAR'] == None: os.environ['ENV_VAR'] ='value' else: print("Environment Variable Exists")
One way to get the value of an environment variable is to use the Python dictionary notation statement we used to set the value of the variable: os.environ['ENV_VAR']
returns the value assigned to the ENV_VAR
Another way is to use the dictionary get()
method to get the value of an environment variable os.environ.get('ENV_VAR')
A third way of getting the value of an environment variable is to use os.environ.setdefault()
method. This method is similar to the get()
one described above, but it takes two required parameters: the variable name we are looking for and a default value if the variable does not exist.
os.environ.setdefault('ENV_VAR', 'Variable Does not Exist')
Someone might ask, what about the os.getenv()
method? What does it do, and why use it instead of using the dictionary notation
statement?
The main reason is because the dictionary notation method will raise an exception when the variable does not exist, and if not handles correctly, the application will crash. On the other hand, os.getenv()
will return None
(default behavior) if the environment variable does not exist. We can set the default value to something more appropriate by specifying an optional string after the variable name: os.getenv('ENV_VRR', 'Variable Does Not Exist')
One might say that os.getenv
is an enhanced version of os.environ.get
with None
the default value, where os.environ.setdefault
requires a default value.
Since we used the getenv()
to get the environment variable’s value, we think to use setenv()
to set a new environment variable. We try the command confident it will work:
os.setenv('NewVar', 'VALUE')
We get an error message instead: module 'os' has no attribute 'setenv.'
We probably misspelled the name; therefore, we try dir(os)
and go through all methods and attributes listed. There is no setenv()
method. The closer you find is putenv()
, and we give it a try: os.putenv('NewVar', 'VALUE')
It looks like it is working; we do not receive any error messages. We run the commands described above, looking for the variable create, but the variable is not there.
Python docs advise using os.environ
to create and assign values to environment variables.
” Assignments to items in os.environ
are automatically translated into corresponding calls to putenv()
; however, calls to putenv()
don’t update os.environ
, so it is actually preferable to assign to items of os.environ
.” (https://docs.python.org/3/library/os.html)
So, what is the purpose of the putenv()
method?
The __setitem__()
method uses it to set the environment variable’s value.
def __setitem__(self, key, value): key = self.encodekey(key) value = self.encodevalue(value) putenv(key, value) self._data[key] = value
We can see that when we create an invalid assignment statement such as the following one:
os.environ['ENV_VAR'] ='value' if 'ENV_VAR' not in os.environ else print("Wrong Value")
How to Print an Environment Variable in Python?
To iterate through all the environment variables, we can use a for loop:
for var in os.environ: print(var)
For a Windows 10 environment, a partial list of the environment variable is the following:
ALLUSERSPROFILE | PATH | TEMP |
APPDATA | PROGRAMDATA | USERNAME |
HOMEDRIVE | PROGRAMFILES | USERPROFILE |
HOMEPATH | PUBLIC | WINDIR |
These variables are useful for setting, accessing, and storing data at a location where the user still has access even when roaming around on the LAN/WAN.
The “for” statement just used provides an unsorted list of all environment variables. To have the variables listed in ascending order, we can use the sorted()
function:
for var in sorted(os.environ): print(var)
Using reverse=True
will display the list in descending order:
for var in sorted(os.environ, reverse=True):
Note: reverse=False
is the default value of the sorted()
function, and there is no need to specify it.
What about getting the values of each variable? How can the associated values be displayed?
There are a couple of ways we can use to get the associated values. Since the os.environ returns a dictionary, and the for
statement we used above displays the key
of each entry in the dictionary, we can use the dictionary notation to return the value of each key:
for var in sorted(os.environ): print(var, os.environ[v])
or alternately use the get dictionary method
for var in sorted(os.environ): print(var, os.environ.get(var))
We can use the familiar string formatting styles to display the list any way we like. To display the Environment variable list in a similar way displayed when we use the set
Windows command, we can use the f
format: print(f"{var} = {os.environ[var]}")
It is one way. Another way is to use the items()
collection instead:
for v, k in os.environ.items(): print(f"{v} = {k}")
We can also convert the environment variables dictionary into a list of tuples and slice it to get a subset of the environment variables, e.g. the first 10 variables:
envl=list(os.environ.items()) for v, k in envl[0:10]: print(f"{v} = {k}")
With so many ways to get the key-value pair of the environment variables, we wonder which one is the better way. We believe efficiency should be the primary reason for selecting one statement over another. One way of timing a piece of code is to use Jupyter Notebook cell magic %%time
.
Therefore we precede the three for statements described above with the %%time
, and we see that the for statement using os.environ.items()
is the slowest one, where the other two have the same execution time.
%%time for v, k in os.environ.items(): print(f"{v} = {k}")
The above for loop is slower by 1 ms. For this case, it does not matter since it is still really fast. However, it is something to keep in mind since calls to the os.environ
are slower and more expensive than a local library, as we demonstrated.
How to Find Your Python Path?
One of the environment variables that tend to become crowded and very long is the PATH one. Older versions of Windows had a limit of 256 characters for the Path environment variable. The path limit Windows had was removed, and with the newer Windows versions, the PATH length can be much longer. To read the PATH variable value you can use os.environ['PATH']
or os.getenv('PATH')
. The string returned will be a very long one and not so easy to read in both cases. To get the path entries in a more usable format, we need to convert the string os.environ['PATH']
into a list:
path_list = os.environ['PATH'].split(';') # or path_list = os.getenv('PATH').split(';')
Note: The Windows Operating system uses semicolon (;) as the path separator, where Linux uses the colon (:).
It sounds like we are hardcoding, right? How can we avoid hardcoding the separator from the code above?
If we list all the attributes and methods of the os
module we will see there is a path separator attribute pathsep
. Therefore, the above code becomes:
path_list = os.environ['PATH'].split(os.pathsep) or path_list = os.getenv('PATH').split(os.pathsep)
We don’t have to worry about the separator anymore, and the application can move to a different operating system with any issues related to the path separator character.
If we like to get the path where Python is installed, we can use the sys.prefix
string as follows:
import sys print(sys.prefix)
The sys.prefix
string tell us the location of the Python installation files, otherwise known as the Python installation path.
One would expect to find an environment variable such as PYTHONPATH with the value provided by sys.prefix
. However, the PYTHONPATH variable does not exist. We can use the sys.prefix
information to create an environment variable PYTHONPATH and access it via the os.environ
instance class:
os.environ['PYTHONPATH'] = sys.prefix # Set environment variable os.environ['PYTHONPATH'] # Get the variable value for p in sorted(os.environ): # Verify the os.environ has been updated print(p)
How to Delete an Environment Variable?
WE DO NOT RECOMMEND DELETING ENVIRONMENT VARIABLES SET BY OTHER APPLICATIONS OR THE OPERATING SYSTEM.
What about when we need to uninstall the application, and we like to clean up the environment?
The moment we decide to create an environment variable, we allow other applications to use it. Therefore, unless we are absolutely sure the environment variable we created is not used by other applications, we should avoid deleting it.
Since the os.environ
is a dictionary with all the dictionary methods and attributes, deleting an environment variable can be done by using the .pop()
method.