20

I have some instrument which requires environment variable which I want to set automatically from python code. So I tried several ways to make it happen, but none of them were successful. Here are some examples:

  1. I insert following code in my python script

     import os
     os.system("export ENV_VAR=/some_path")
    
  2. I created bash script(env.sh) and run it from python:

     #!/bin/bash
     export ENV_VAR=some_path
    
     #call it from python
     os.system("source env.sh")
    
  3. I also tried os.putenv() and os.environ*["ENV_VAR"] = "some_path"

Is it possible to set(export) environment variable using python, i.e without directly exporting it to shell?

3
  • Is this "instrument" being started from within the python script? Commented Aug 16, 2016 at 19:08
  • Yes it is for that reason I also need to change my PATH env var. Commented Aug 16, 2016 at 21:17
  • All processes have an environment block regardless of which language they are written in - you don't need a shell. Environment blocks are (by default) copied from parent to child when you fork() - they are not copied at any other time. Commented Aug 16, 2016 at 21:21

4 Answers 4

33

Setting an environment variable sets it only for the current process and any child processes it launches. So using os.system will set it only for the shell that is running to execute the command you provided. When that command finishes, the shell goes away, and so does the environment variable. Setting it using os.putenv or os.environ has a similar effect; the environment variables are set for the Python process and any children of it.

I assume you are trying to have those variables set for the shell that you launch the script from, or globally. That can't work because the shell (or other process) is not a child of the Python script in which you are setting the variable.

You'll have better luck setting the variables in a shell script. If you then source that script (so that it runs in the current instance of the shell, rather than in a subshell) then they will remain set after the script ends.

Sign up to request clarification or add additional context in comments.

1 Comment

how would I re-create export FOO=bar as a shell command in python then
14

As long as you start the "instrument" (a script I suppose) from the very same process it should work:

In [1]: os.putenv("VARIABLE", "123")

In [2]: os.system("echo $VARIABLE")
123

You can't change an environment variable of a different process or a parent process.

Comments

13

A shell function may do this. You need to print your export statement and eval that.

set_shell_env() {
    output=$(python print_export_env.py $*)
    eval $output
}

5 Comments

The only good answer for this problem! The other guys and gals here just try to educate the asker. No one asked about right and wrong... we just want to set env variables in python... what so hard to understand?!
Good except that it should be"$@", not $* -- right now arguments are getting word-split on spaces and expanded as globs. And similarly, eval "$output", not eval $output.
this is why I love this site. completely unsafe answer that works perfectly
what's expected return content in python script?
@LeiYang anything which can be evaluated by eval in a shell script, so bash code. And @CharlesDuffy - if you wrap the output in double quotes, it makes it hard to process multi-line outputs. Only wrap the output in double quotes if you're going to loop through each line and eval them one by one
0

Depending on how you execute your instrument, you might be able to change environment specifically for the child process without affecting the parent. See documentation for os.spawn*e or subprocess.Popen which accept separate argument denoting child environment. For example, Replacing the os.spawn family in subprocess module documentation which provides both usages:

Environment example:

os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
==>
Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.