Dave S
Dave S

Reputation: 1009

How to pass 'env' var to a Python script via an arg in subprocess.Popen

I am writing an Python wrapper where a Python script creates a custom env variable by adding a large number of elements.

For example:

env['DEBUG'] = '1'
env['TBB_NUM_THREADS'] = str(args.threads)
...

This first wrapper calls a second wrapper via subprocess.Popen like this:

command = ['wrapper2.py'] + args
subprocess.Popen(command, env=env).wait()

I need the second wrapper to have the same env as the first. Ideally, I would like to modify the above assignment so the second argument is the env. In this way the second script can easily access it and set its env to that of the first script.

command = ['wrapper2.py'] + env + args

But this causes the following error: "Typeerror: can only concatenate list (not "instance") to list"

What would be the best way to approach this problem? Note: I am using Python 2.7

Upvotes: 0

Views: 340

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295696

It's an ugly hack, but if you're unwilling to pass env out-of-band from args as a separate argument, you can use the env UNIX utility to set your environment variables instead of using the subprocess env facility.

That is:

args = [ 'env', 'DEBUG=1', 'TBB_NUM_THREADS=%s' % (arg_threads),
         './wrapper2.py' ] + wrapper2_args

A less-ugly hack is to pass around a single kwargs list that contains both:

kwargs = {
  'args': [ './wrapper2.py' ] + wrapper2_args,
  'env':  { "DEBUG": "1", "TBB_NUM_THREADS": str(arg_threads), }
}

...and then pass it in using **kwargs syntax:

p = subprocess.Popen(**kwargs)

Upvotes: 2

Related Questions