Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the environment variable set by a bash script from inside a python script?

I do have a setenv.sh script which is called at the begining of several others scripts, most of them being bash scripts. Obviously this script does set some enviroment variables, which are user later by these scripts.

Now, the problem is that I want to implement the same behaviour into some python scripts and I discovered that the enviroment of the python script is not updated if you run the setenv.

As I do not want to create another script which calls first the setenv.sh and other this myscript.py --- I am looking for a way to convince python to load these variables (parsing the seteve.h is not an option... as it is more complex).

like image 453
sorin Avatar asked Jan 26 '26 16:01

sorin


2 Answers

Here's a little library that does it:

https://github.com/aneilbaboo/shellvars-py

like image 103
Aneil Mallavarapu Avatar answered Jan 28 '26 12:01

Aneil Mallavarapu


The easiest solution is clearly the one you don't want, which is to create a new script file for each python script.

You could, however, do roughly the equivalent by having the python script call itself. Of course, you need to signal it to not do that on the second invocation, otherwise you'll end up with an infinite (tail) recursion.

The following little "module" (which you can just import, but you should do it right at startup, before anything else) will check to see if the environment variable SETENV has been set, and if so, it will re-issue the python command (to the best of its ability, so it might get things wrong if it wasn't just a simple script execution) after sourcing the file named by SETENV. It lacks lots of error-checking and shouldn't be considered production-ready; rather a proof-of-concept:

# file env_set.py
import os
import sys

if sys.argv[0] and "SETENV" in os.environ:
  setenv = os.environ["SETENV"]
  del os.environ["SETENV"]
  os.execvp("bash", ["bash", "-c",
        "source " + setenv + "; exec python " + sys.argv[0] + ' "${@}"',
        "--"] + sys.argv[1:])

And a little test:

# file test_env_set.py
import env_set

import os
import sys
for name in sys.argv[1:]:
  if name in os.environ:
    print(name + "=" + os.environ[name])
  else:
    print("Undefined: " + name)

# file setenv.sh
export the_answer=42

$ python test_env_set.py SETENV the_answer
Undefined: SETENV
Undefined: the_answer
$ SETENV=setenv.sh python test_env_set.py SETENV the_answer
Undefined: SETENV
the_answer=42
like image 26
rici Avatar answered Jan 28 '26 12:01

rici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!