0

I am modifying the environmental variable TMPDIR inside of a python script so a bash script can use it as a working space. Here is a sample of how I am doing it.

import subprocess as sp
import os

os.environ['TMPDIR'] = "./tmp"
print "Use python to set the tmpdir to:"
print os.environ['TMPDIR']
sp.Popen('bash test_bash',shell=True)

for testing my script test_bash just has

#! /bin/sh

echo "test_bash should show TMPDIR Path here:"
echo "----> $TMPDIR"

The output is

Use python to set the tmpdir to:
./tmp
test_bash should show TMPDIR Path here:
---->

When I change the name variable TMPDIR to anything else, my code works.

Does sp.Popen() protect $TMPDIR? And if so is there a right way I can modify it?

I would just use another variable name except I am working on a large project where $TMPDIR is used heavily in both python and bash. The goal is to use this in a shared environment and allow users to specify a working dir to avoid over-writing each other.

Things I have tried:

  • Passing the env as answered in this question
  • Updating to subprocess32, which seemed like it might help based on the description on the pypi page but I might be confused.
  • Use os.path.abspath('./tmp) to ensure a full path is set.
Community
  • 1
  • 1
Micah Johnson
  • 646
  • 5
  • 8
  • 1
    should work. Have you tried passing `env` explicitly: `sp.Popen('bash test_bash',shell=True,env=os.environ)` – Jean-François Fabre Jan 31 '17 at 15:44
  • I'm not sure if there is a race condition here; using `shell=True` causes an `sh` process to be forked off to run the `bash` command, and your python script can exit before that process runs. It shouldn't matter (the environment should be copied to the child before the parent exits), but try using `sp.Popen("bash test_bash", shell=True).wait()`. You might also try eliminating the middle process by passing a list: `sp.Popen(["bash", "test_bash"])` (both with and without `wait()`). – chepner Jan 31 '17 at 16:22
  • @Jean-FrançoisFabre I have passed the env explicitly with no improvement. – Micah Johnson Jan 31 '17 at 16:28
  • @chepner test_bash sp.Popen(["bash", "test_bash"]) appears to create the race condition. When I add wait() is seems to try and run two separate commands and test_bash is not executable so it fails. Not sure what I might be doing wrong. – Micah Johnson Jan 31 '17 at 16:52

0 Answers0