0

I am writing a program that needs to spawn a new terminal window and launch a server in this new terminal window (with environment variables passed to the child process).

I have been able to achieve this on windows 10 and linux without much trouble but on Mac OS X (Big Sur) the environment variables are not being passed to the child process. Here is an example code snippet capturing the behaviour I want to achieve:

#!/usr/bin/python3
import subprocess
import os
command = "bash -c 'export'"
env = os.environ.copy()
env["MYVAR"] = "VAL"

process = subprocess.Popen(['osascript', '-e', f"tell application \"Terminal\" to do script \"{command}\""], env=env)

Unfortunately, MYVAR is not present in the exported environment variables.

Any ideas if I am doing something wrong here?

Is this a bug in python's standard library ('subprocess' module)?

edit - thank you Ben Paterson (previously my example code had a bug) - I have updated the code example but I still have the same issue.

edit - I have narrowed this down further. subprocess.Popen is doing what it is supposed to do with environment variables when I do:

command = "bash -c 'export > c.txt'"
process = subprocess.Popen(shlex.split(command, posix=1), env=env)

But when I try to wrap the command with osascript -e ... (to spawn it in a new terminal window) the environment variable "MYVAR" does not appear in the c.txt file.

command = "bash -c 'export > c.txt'"
process = subprocess.Popen(['osascript', '-e', f"tell application \"Terminal\" to do script \"{command}\""], env=env)

1 Answer 1

1

dict.update returns None, so the OP code is equivalent to passing env=None to subprocess.Popen. Write instead:

env = os.environ.copy()
env["MYVAR"] = "VAL"
subprocess.Popen(..., env=env)
4
  • Thanks for your prompt response! I have updated the code example to remove the bug. Sorry about that. But I still have the same problem. I wasn't making this same mistake in my actual code either... Are you able to get it to print out MYVAR in the new terminal on Big Sur?
    – AustEcon
    Dec 27, 2020 at 23:57
  • briandfoy.github.io/… seems like a possible culprit. Dec 28, 2020 at 0:07
  • Thanks will take a look. I have narrowed it down to this: This works: command = "bash -c 'export > c.txt'" process = subprocess.Popen(shlex.split(command, posix=1), env=env) but this does not: ` command = "bash -c 'export > c.txt'" process = subprocess.Popen(['osascript', '-e', f"tell application \"Terminal\" to do script \"{command}\""], env=env) (excuse the bad code formatting.. But seems like it is only an issue when wrapping the command with osascript...
    – AustEcon
    Dec 28, 2020 at 0:19
  • I will accept your answer for now but I still have an unresolved issue with passing environment variables to osascript calls. If you know how to do that (or beat me to it figuring it out) that would make your final answer perfect & complete. Thanks.
    – AustEcon
    Dec 28, 2020 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.