1

I have a driver program which launches and manages hundreds of processes by

p = subprocess.Popen(cmd)

One of the situation I'm facing with is: since many processes may set-up their own environment variable by

os.environ[key] = val

which may disappear after the process exits.

My question is: since some of them may fail, I want to reproduce them locally, so I need the environment variables for the correct if-else path.

Is there a way in python to get env vars from subprocess?

2
  • No. If you have a way to identify each process, you can have the subprocess dump its environment to a file. Commented Oct 21, 2021 at 3:58
  • Environment variables are process-local. They're gone when the program that sets them and its children have exited. That's always true, by-design. Commented Oct 21, 2021 at 17:02

1 Answer 1

1

which may disappear after the process exits

That should be "which will disappear". In the UNIX process model env vars are private to each process. If you're on Linux you might be tempted to try reading /proc/$pid/environ. That however would be incorrect since it only lists the vars that were present when the process was created. It will not include any changes the process made to its own env vars.

In short there is no way to do what you want without the explicit cooperation of the child process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.