Getting, adding, overwriting, and deleting environment variables in Python (os.environ)

Money and Business

Environment variables can be retrieved, checked, set (added or overwritten), and deleted in Python programs using os.environ. Note that changes made by setting or deleting environment variables are effective only within the Python program. It does not mean that the system environment variables will be rewritten.

The following information is provided here.

  • os.environ
  • Get environment variables.
  • Set (add/overwrite) environment variables
  • Remove environment variables
  • Effect of changing environment variables
  • Switching processes by environment variables

Import and use the os module. Since it is a standard library, no additional installation is required. The subprocess module is also included in the standard library.

import os
import subprocess

os.environ

The type of os.environ is os._Environ.

print(type(os.environ))
# <class 'os._Environ'>

os._Environ is a map type object with a pair of key and value, and has the same methods as a dictionary (dict type). The environment variable name is key, and its value is value.

The contents of os.environ will be loaded when the os module is imported. The contents of os.environ will not be updated even if the system environment variables are changed by other means while the program is running.

The list is displayed with print().

# print(os.environ)

As with the dictionary, you can use the following methods, or use in to check for the existence of keys and values.

  • keys()
  • values()

The processing of keys and values is basically the same as for dictionaries. Examples are given below.

Get environment variables.

os.environ[Environment variable name]
This will allow you to get the value of the environment variable, but if you specify an environment variable name that does not exist, you will get an error (KeyError).

print(os.environ['LANG'])
# ja_JP.UTF-8

# print(os.environ['NEW_KEY'])
# KeyError: 'NEW_KEY'

The get() method of os.environ can be used to get the default value if it does not exist. This is also the same as the dictionary.

print(os.environ.get('LANG'))
# ja_JP.UTF-8

print(os.environ.get('NEW_KEY'))
# None

print(os.environ.get('NEW_KEY', 'default'))
# default

The function os.getenv() is also provided. Like the get() method of the dictionary, it returns the default value if the key does not exist. This function is useful if you just want to get and check the value of an environment variable.

print(os.getenv('LANG'))
# ja_JP.UTF-8

print(os.getenv('NEW_KEY'))
# None

print(os.getenv('NEW_KEY', 'default'))
# default

Set (add/overwrite) environment variables

os.environ[Environment variable name]
By assigning a value to this, you can set an environment variable.

When a new environment variable name is specified, the environment variable is newly added, and when an existing environment variable name is specified, the value of the environment variable is overwritten.

os.environ['NEW_KEY'] = 'test'

print(os.environ['NEW_KEY'])
# test

os.environ['NEW_KEY'] = 'test2'

print(os.environ['NEW_KEY'])
# test2

Note that assigning anything other than a string will result in an error (TypeError). If you want to assign a numerical value, specify it as a string.

# os.environ['NEW_KEY'] = 100
# TypeError: str expected, not int

os.environ['NEW_KEY'] = '100'

The function os.putenv() is also provided. However, the value of os.environ is not updated when it is set by os.putenv(). For this reason, it is preferable to specify the key (environment variable name) of os.environ and assign the value as shown in the example above.

If putenv() is supported, an assignment to an item in os.environ will automatically be converted to a corresponding call to putenv(). In practice, assigning to an item in os.environ is the preferred operation, since a direct call to putenv() will not update os.environ.
os.putenv() — Miscellaneous operating system interfaces — Python 3.10.0 Documentation

As mentioned earlier, changes made by adding or overwriting environment variables are effective only within the Python program. It does not mean that the system environment variables will be rewritten.

Note that changing the value may cause a memory leak depending on the OS.

Note: On some platforms, including FreeBSD and Mac OS X, changing the value of environ may cause a memory leak.
os.putenv() — Miscellaneous operating system interfaces — Python 3.10.0 Documentation

This is due to the putenv() specification of the OS itself.

Successive calls to setenv() or putenv() assigning a differently sized value to the same name will result in a memory leak. The FreeBSD seman-tics semantics for these functions (namely, that the contents of value are copied and that old values remain accessible indefinitely) make this bug unavoidable.
Mac OS X Manual Page For putenv(3)

Remove environment variables

To delete an environment variable, use the pop() method of os.environ or the del statement. Same as dictionary.

The following is an example of pop().

pop() returns the value of the environment variable that was deleted. By default, specifying an environment variable that does not exist will result in an error (KeyError), but specifying the second argument will return the value of the environment variable if it does not exist.

print(os.environ.pop('NEW_KEY'))
# 100

# print(os.environ.pop('NEW_KEY'))
# KeyError: 'NEW_KEY'

print(os.environ.pop('NEW_KEY', None))
# None

The following is an example of del.

The environment variable is added again, and then deleted. If the environment variable does not exist, an error (KeyError).

os.environ['NEW_KEY'] = '100'

print(os.getenv('NEW_KEY'))
# 100

del os.environ['NEW_KEY']

print(os.getenv('NEW_KEY'))
# None

# del os.environ['NEW_KEY']
# KeyError: 'NEW_KEY'

The function os.unsetenv() is also provided. However, as with os.putenv(), the value of os.environ is not updated when it is deleted by os.unsetenv(). Therefore, it is preferable to specify the key (environment variable name) of os.environ and delete it as shown in the example above.

If unsetenv() is supported, deleting an item in os.environ will automatically translate to the corresponding call to unsetenv(). In practice, deleting items in os.environ is the preferred operation, since direct calls to unsetenv() will not update os.environ.
os.unsetenv() — Miscellaneous operating system interfaces — Python 3.10.0 Documentation

Deleting environment variables is also effective only within that Python program. It does not remove the system environment variables.

Effect of changing environment variables

As I have written repeatedly, changing (setting or deleting) the os.environ environment variable does not change the system environment variable, but it does affect the sub-processes that are launched in the program.

The following code will not work as expected on Windows because there is no LANG environment variable and the contents of the date command are different.

Calling the date command in the subprocess module.

The output result of the date command changes depending on the value of the LANG environment variable.

print(os.getenv('LANG'))
# ja_JP.UTF-8

print(subprocess.check_output('date', encoding='utf-8'))
# 2018年 7月12日 木曜日 20時54分13秒 JST
# 

os.environ['LANG'] = 'en_US'

print(subprocess.check_output('date', encoding='utf-8'))
# Thu Jul 12 20:54:13 JST 2018
# 

For the sake of explanation, we have changed the LANG environment variable in os.environ, but Python provides a locale module to control the locale.

Switching processes by environment variables

It is also possible to switch the process according to the value of an environment variable.

Here is an example of changing the output according to the LANG environment variable in the language settings. Here we are using the startswith() method to determine if the string starts with the specified string, but if you want to determine the exact match, you can use “==” to compare.

print(os.getenv('LANG'))
# en_US

if os.getenv('LANG').startswith('ja'):
    print('こんにちは')
else:
    print('Hello')
# Hello

os.environ['LANG'] = 'ja_JP'

if os.getenv('LANG').startswith('ja'):
    print('こんにちは')
else:
    print('Hello')
# こんにちは

In addition, if environment variables are set to indicate the development environment and the production environment, for example, you can get the values of these variables and switch the process.

Copied title and URL