1

Running on Ubuntu, I have a python script, written by someone else, which simply runs like this:

python cool_script.py command line args

However, this script needs some environment variables set before it can run. So I created exports.sh located in the same dir as the python script, which looks something like:

export ENV1='foo'
export ENV2='bar'
export ENV3='baz'

so running:

$source exports.sh
$python cool_script.py command line args

works great. However, my case is a bit more complex. I need to run this on several environments, where the values of ENV1,ENV2,ENV3 will have to be different. I also want to do some stuff to the outputs of cool_script.py. So I wrote cool_script_wrapper.py, which looks like this:

# set environment variables
import os
exports_file = os.path.dirname(os.path.realpath(__file__)) + '/exports.sh'
os.system('source %s' % exports_file)
# run cool_script.py
os.system('python cool_script.py command line args')
# do some stuff to the output
...

I was planning to have different exports.sh scripts for each environment I need to run on, always keeping them in the same dir as the main script. Only problem is that this 'source' approach doesn't work. The environment variables are simply not available to cool_script.py.
Searching around Stack Overflow, I understand now that it's not supposed to work, but I didn't find any solution that suits my needs. I don't want to use os.environ with hard-coded values, and I don't want to parse the exports file. I guess I could make my wrapper a bash script rather than a python, but I hope there is a better solution. Any ideas?

soungalo
  • 1,106
  • 2
  • 19
  • 34
  • 1
    Why are you doing this via environment variables? Why not make the wrapper script import the *actual functions* and call them directly with the appropriate arguments? – jonrsharpe Dec 17 '16 at 12:21
  • @jonrsharpe - sorry, but I don't understand what you mean. Can you explain in more detail or give an example? – soungalo Dec 17 '16 at 12:25
  • Well not really, as you don't show any detail of the program you're talking about. But if it's sensibly structured, you should be able to `from cool_script import useful_func` and then call the function directly with whatever values you'd have put into the environment and passed over the command line. Why not talk to the person who wrote it, if it's unclear how best to interact with it? – jonrsharpe Dec 17 '16 at 12:26
  • Why would I do that? I want cool_script.py to run as is, start to end. And anyway, I still don't understand how this will solve my problem. – soungalo Dec 17 '16 at 12:29
  • 1
    1. Why? Please give some context. 2. By *removing* the problem entirely, as you won't need to set the environment variables. – jonrsharpe Dec 17 '16 at 12:51

1 Answers1

0

The problem is in the source command that attempts to call a bash builtin. This should work:

import os
os.environ['ENV1'] = "foo"
os.system("python cool_script.py command line args")

compare with this answer: Calling the "source" command from subprocess.Popen

Community
  • 1
  • 1
Schuh
  • 1,045
  • 5
  • 9
  • Thanks, but as I said, in other environments on which I wish to run ENV1 will have to take a different value, say 'bob'. That's why I don't want to put hard-coded values within my script. – soungalo Dec 17 '16 at 13:27
  • So then read the value from the respective file that you put in the directory, the same strategy like with the '/exports.sh'. – Schuh Dec 17 '16 at 13:39