[ACCEPTED]-How to get environment from a subprocess?-popen
Here's an example of how you can extract 4 environment variables from a batch or cmd 3 file without creating a wrapper script. Enjoy.
from __future__ import print_function
import sys
import subprocess
import itertools
def validate_pair(ob):
try:
if not (len(ob) == 2):
print("Unexpected result:", ob, file=sys.stderr)
raise ValueError
except:
return False
return True
def consume(iter):
try:
while True: next(iter)
except StopIteration:
pass
def get_environment_from_batch_command(env_cmd, initial=None):
"""
Take a command (either a single command or list of arguments)
and return the environment created after running that command.
Note that if the command must be a batch file or .cmd file, or the
changes to the environment will not be captured.
If initial is supplied, it is used as the initial environment passed
to the child process.
"""
if not isinstance(env_cmd, (list, tuple)):
env_cmd = [env_cmd]
# construct the command that will alter the environment
env_cmd = subprocess.list2cmdline(env_cmd)
# create a tag so we can tell in the output when the proc is done
tag = 'Done running command'
# construct a cmd.exe command to do accomplish this
cmd = 'cmd.exe /s /c "{env_cmd} && echo "{tag}" && set"'.format(**vars())
# launch the process
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=initial)
# parse the output sent to stdout
lines = proc.stdout
# consume whatever output occurs until the tag is reached
consume(itertools.takewhile(lambda l: tag not in l, lines))
# define a way to handle each KEY=VALUE line
handle_line = lambda l: l.rstrip().split('=',1)
# parse key/values into pairs
pairs = map(handle_line, lines)
# make sure the pairs are valid
valid_pairs = filter(validate_pair, pairs)
# construct a dictionary of the pairs
result = dict(valid_pairs)
# let the process finish
proc.communicate()
return result
So 2 to answer your question, you would create 1 a .py file that does the following:
env = get_environment_from_batch_command('proc1')
subprocess.Popen('proc2', env=env)
As you say, processes don't share the environment 7 - so what you literally ask is not possible, not 6 only in Python, but with any programming 5 language.
What you can do is to put the environment 4 variables in a file, or in a pipe, and either
- have the parent process read them, and pass them to proc2 before proc2 is created, or
- have proc2 read them, and set them locally
The 3 latter would require cooperation from proc2; the 2 former requires that the variables become 1 known before proc2 is started.
Since you're apparently in Windows, you 9 need a Windows answer.
Create a wrapper batch 8 file, eg. "run_program.bat", and run both 7 programs:
@echo off
call proc1.bat
proc2
The script will run and set its 6 environment variables. Both scripts run 5 in the same interpreter (cmd.exe instance), so 4 the variables prog1.bat sets will be set when 3 prog2 is executed.
Not terribly pretty, but 2 it'll work.
(Unix people, you can do the 1 same thing in a bash script: "source file.sh".)
The Python standard module multiprocessing have a Queues system that allow you to 8 pass pickle-able object to be passed through 7 processes. Also processes can exchange messages 6 (a pickled object) using os.pipe. Remember 5 that resources (e.g : database connection) and 4 handle (e.g : file handles) can't be pickled.
You 3 may find this link interesting : Communication between processes with multiprocessing
Also the 2 PyMOTw about multiprocessing worth mentioning 1 : multiprocessing Basics
sorry for my spelling
You can use Process
in psutil to get the environment 10 variables for that Process.
If you want to 9 implement it yourself, you can refer to 8 the internal implementation of psutil. It adapts 7 to some operating system.
Currently supported 6 operating systems are:
- AIX
- FreeBSD, OpenBSD, NetBSD
- Linux
- macOS
- Sun Solaris
- Windows
Eg: In Linux platform, you 5 can find one pid 7877 environment variables 4 in file /proc/7877/environ
, just open with rt
mode to read it.
Of 3 course the best way to do this is to:
import os
from typing import Dict
from psutil import Process
process = Process(pid=os.getpid())
process_env: Dict = process.environ()
print(process_env)
You 2 can find other platform implementation in 1 source code
Hope I can help you.
Two things spring to mind: (1) make the 9 processes share the same environment, by 8 combining them somehow into the same process, or 7 (2) have the first process produce output 6 that contains the relevant environment variables, that 5 way Python can read it and construct the 4 environment for the second process. I think 3 (though I'm not 100% sure) that there isn't 2 any way to get the environment from a subprocess 1 as you're hoping to do.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.