Shankar M
Shankar M

Reputation: 3

how to set environmental variables permanently in posix(unix/linux) machine using python script

I am trying to set a environmental variable permanently. but temporarily it is working.

if i run below program i got the variable path. after close it and open new terminal to find the variable path using the command printenv LD_LIBRARY_PATH nothing will print.

#!/usr/bin/python


import os
import subprocess

def setenv_var():
    env_var = "LD_LIBRARY_PATH"
    env_path = "/usr/local/lib"`enter code here`
    os.environ[env_var] = env_path
    process = subprocess.Popen('printenv ' + env_var, stdout=subprocess.PIPE, shell=True)
    result = process.communicate()[0]
    return result

if __name__ == '__main__':
    print setenv_var()

please help me.

Upvotes: 0

Views: 1957

Answers (3)

kartikg3
kartikg3

Reputation: 2620

Whether you do an export from bash or you set your os.environ from Python, these only stay for the session or process's lifetime. If you want to set them permanent you will have to touch and add it to the respective shell's profile file.

For ex. If you are on bash, you could do:

with open("~/.bashrc", "a") as outfile: # 'a' stands for "append"
     outfile.write("export LD_LIBRARY_PATH=/usr/local/lib")

Check this out for some insight as to which file to add this depending on the target shell. https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables

Upvotes: 0

Reut Sharabani
Reut Sharabani

Reputation: 31339

Here is what I use to set environment variables:

def setenv_var(env_file, set_this_env=True):
    env_var = "LD_LIBRARY_PATH"
    env_path = "/usr/local/lib"`enter code here`
    # set environments opened later by appending to `source`-d file
    with open(env_file, 'a') as f:
        f.write(os.linesep + ("%s=%s" % (env_var, env_path)))
    if set_this_end:
        # set this environment
        os.environ[env_var] = env_path

Now you only have to choose where to set it, that is the first argument in the function. I recommend the profile-specific file ~/.profile or if you're using bash which is pretty common ~/.bashrc

You can also set it globally by using a file like /etc/environment but you'll need to have permissions when you run this script (sudo python script.py).

Remember that environments are inherited from the parent process, and you can't have a child set up a parent process' environment.

Upvotes: 1

William Pursell
William Pursell

Reputation: 212268

When you set an environment variable, it only affects the currently running process (and, by extension, any children that are forked after the variable is set). If you are attempting to set an environment variable in your shell and you want that environment variable to always be set for your interactive shells, you need to set it in the startup scripts (eg .login, .bashrc, .profile) for your shell. Commands that you run are (initially) children of the shell from which you run them, so although they inherit the environment of the shell and can change their own environment, they cannot change the environment of your shell.

Upvotes: 1

Related Questions