You're trying to set environment variables from within the python program, that are set in a shell script. I don't think it's possible in any straightforward way. However, you could set up a jacket for your python program that sources the shell script before calling the python program. Then the Python program can access the environment variables as os.environ
, for example:
import os
print(os.environ['HOME'])
The reason it's not possible in a straightforward way is that subprocess
(or any other means to run the shell script) will execute the shell script in a new process, and environment variable settings will be discarded when that process exits.
Another way would be to write another shell script that prints the names and values of all the shell values you need. In your python program, use subprocess
to source both the original script and then your add-on script, and then parse the output to extract the variable names and values. The trickiest part here is handling special characters in values.