7

I have a file with some environment variables that I want to use in a python script

The following works form the command line

$ source myFile.sh
$ python ./myScript.py

and from inside the python script I can access the variables like

import os
os.getenv('myvariable')

How can I source the shell script, then access the variables, from with the python script?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
lafferc
  • 2,741
  • 3
  • 24
  • 37
  • What do you mean? Do you mean how do you *set* environment variables? – jonrsharpe Jun 29 '15 at 13:55
  • i want to call 'source myFile.sh' from inside the python script and then access the variables – lafferc Jun 29 '15 at 13:58
  • So you just want to know how to run a command from Python? Have you tried searching for *that*? *"load environment variables"* seems **completely incidental** to what you're trying to achieve. – jonrsharpe Jun 29 '15 at 14:00
  • I know how to run a bash command from python, but is there a built in way of sourcing the file ? – lafferc Jun 29 '15 at 14:01
  • 1
    related: [Calling the “source” command from subprocess.Popen](http://stackoverflow.com/q/7040592/4279) – jfs Oct 14 '15 at 06:42
  • Possible duplicate of [Emulating Bash 'source' in Python](https://stackoverflow.com/questions/3503719/emulating-bash-source-in-python) – sds Jun 26 '17 at 14:07

2 Answers2

9

If you are saying backward environment propagation, sorry, you can't. It's a security issue. However, directly source environment from python is definitely valid. But it's more or less a manual process.

import subprocess as sp

SOURCE = 'your_file_path'
proc = sp.Popen(['bash', '-c', 'source {} && env'.format(SOURCE)], stdout=sp.PIPE)

source_env = {tup[0].strip(): tup[1].strip() for tup in map(lambda s: s.strip().split('=', 1), proc.stdout)}

Then you have everything you need in source_env.

If you need to write it back to your local environment (which is not recommended, since source_env keeps you clean):

import os

for k, v in source_env.items():
    os.environ[k] = v

Another tiny attention needs to be paid here, is since I called bash here, you should expect the rules are applied here too. So if you want your variable to be seen, you will need to export them.

export VAR1='see me'
VAR2='but not me'
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • 1
    That's not going to work properly for all possible environment variable values -- the output of `env` isn't intended to be unambiguously parsed. – Charles Duffy Jun 23 '17 at 20:49
  • 1
    @CharlesDuffy true. there are corner cases. one possible way to actually deal with it will need another script that prints `k=v` delimited by `\0`. – Jason Hu Jun 23 '17 at 22:08
  • Indeed. One could even `cat /proc/self/environ`, which will emit exactly that format, if the only OS that needs to be supported is Linux. :) – Charles Duffy Jun 23 '17 at 22:12
-3

You can not load environmental variables in general from a bash or shell script, it is a different language. You will have to use bash to evaluate the file and then somehow print out the variables and then read them. see Forcing bash to expand variables in a string loaded from a file

Community
  • 1
  • 1