Robert Strauch
Robert Strauch

Reputation: 12896

Change environment variables persistently with Python

Is it possible using Python 3.5 to create and update environment variables in Windows and Linux so that they get persisted?

At the moment I use this:

import os
os.environ["MY_VARIABLE"] = "TRUE"

However it seems as if this does not "store" the environment variable persistently.

Upvotes: 6

Views: 7655

Answers (2)

ktbiz
ktbiz

Reputation: 586

The standard way to 'persist' an environment variable is with a configuration file. Write your application to open the configuration file and set every NAME=VARIABLE pair that it finds. Optionally this step could be done in a wrapper startup script.

If you wish to 'save' the state of a variable, you need to open the configuration file and modify its contents. Then when it's read in again, your application will set the environment accordingly.

You could of course store the configuration in some other way. For example in a configuration_settings class that you pickle/shelve. Then on program startup you read in the pickled class and set the environment. The important thing to understand is that when a process exits its environment is not saved. Environments are inherited from the parent process as an intentional byproduct of forking.

Config file could look like:

NAME=VALUE
NAME2=VALUE2
...

Or your config class could look like:

class Configuration():
    env_vars = {}
    import os
    def set(name, val):
        env_vars[name] = val
        os.environ[name] = val
    def unset(name):
        del env_vars[name]
        del os.environ[name]
    def init():
        for name in env_vars:
            os.environ[name] = env_vars[name]

Somewhere else in our application

import shelve
filename = "config.db"
d = shelve.open(filename)

# get our configuration out of shelve
config = d['configuration']

# initialize environment
config.init()

# setting an environment variable
config.set("MY_VARIABLE", "TRUE")

#unsetting
config.unset("MY_VARIABLE")

# save our configuration
d['configuration'] = config

Code is not tested but I think you get the jist.

Upvotes: 5

AaronI
AaronI

Reputation: 862

I'm speaking for Linux here, not sure about Windows.

Environment variables don't work that way. They are a part of the process (which is what you modify by changing os.environ), and they will propagate to child processes of your process (and their children obviously). They are in-memory only, and there is no way to "set and persist" them directly.

There are however several configuration files which allow you to set the environment on a more granular basis. These are read by various processes, and can be system-wide, specific to a user, specific to a shell, to a particular type of process etc.

Some of them are:

  • /etc/environment for system-wide variables
  • /etc/profile for shells (and their children)
  • Several other shell-specific files in /etc
  • Various dot-files in a user's home directory such as .profile, .bashrc, .bash_profile, .tcshrc and so on. Read your shell's documentation.
  • I believe that there are also various ways to configure environment variables launched from GUIs (e.g. from the gnome panel or something like that).

Most of the time you'll want to set environment variables for the current user only. If you only care about shells, append them to ~/.profile in this format:

NAME="VALUE"

Upvotes: 8

Related Questions