Read Bash variables into a Python script

bash, environment-variables, python, scripting

Solution

You need to export the variables in bash, or they will be local to bash:

export test1

Then, in python

import os
print os.environ["test1"]

Problem

I am running a bash script (test.sh) and it loads in environment variables (from env.sh). That works fine, but I am trying to see python can just load in the variables already in the bash script. Yes I know it would probably be easier to just pass in the specific variables I need as arguments, but I was curious if it was possible to get the bash variables. test.sh ``` #!/bin/bash source env.sh echo $test1 python pythontest.py ``` env.sh ``` #!/bin/bash test1="hello" ``` pythontest.py ``` ? print test1 (that is what I want) ```

Original source

Related problems