是否可以在python中更改父进程的环境?

时间:2022-06-21 22:42:29

In Linux When I invoke python from the shell it replicates its environment, and starts the python process. Therefore if I do something like the following:

在Linux中当我从shell调用python时,它复制了它的环境,并启动了python进程。因此,如果我执行以下操作:

import os
os.environ["FOO"] = "A_Value"

When the python process returns, FOO, assuming it was undefined originally, will still be undefined. Is there a way for the python process (or any child process) to modify the environment of its parent process?

当python进程返回时,FOO(假设它原来是未定义的)仍将是未定义的。有没有办法让python进程(或任何子进程)修改其父进程的环境?

I know you typically solve this problem using something like

我知道你通常用类似的东西来解决这个问题

source script_name.sh

But this conflicts with other requirements I have.

但这与我的其他要求相冲突。

3 个解决方案

#1


17  

No process can change its parent process (or any other existing process' environment).

没有进程可以更改其父进程(或任何其他现有进程的环境)。

You can, however, create a new environment by creating a new interactive shell with the modified environment.

但是,您可以通过使用已修改的环境创建新的交互式shell来创建新环境。

You have to spawn a new copy of the shell that uses the upgraded environment and has access to the existing stdin, stdout and stderr, and does its reinitialization dance.

您必须生成一个使用升级环境的shell的新副本,并且可以访问现有的stdin,stdout和stderr,并进行重新初始化。

You need to do something like use subprocess.Popen to run /bin/bash -i.

你需要做一些事情,比如使用subprocess.Popen运行/ bin / bash -i。

So the original shell runs Python, which runs a new shell. Yes, you have a lot of processes running. No it's not too bad because the original shell and Python aren't really doing anything except waiting for the subshell to finish so they can exit cleanly, also.

所以原始的shell运行Python,它运行一个新的shell。是的,你有很多进程在运行。不,它不是太糟糕,因为原来的shell和Python并没有真正做任何事情,除了等待子shell完成,所以他们也可以干净利落地退出。

#2


13  

It's not possible, for any child process, to change the environment of the parent process. The best you can do is to output shell statements to stdout that you then source, or write it to a file that you source in the parent.

对于任何子进程,都不可能改变父进程的环境。您可以做的最好的事情是将shell语句输出到您之后获取的stdout,或将其写入您在父项中提供的文件。

#3


11  

I would use the bash eval statement, and have the python script output the shell code

我会使用bash eval语句,并让python脚本输出shell代码

child.py:

child.py:

#!/usr/bin/env python
print 'FOO="A_Value"'

parent.sh

parent.sh

#!/bin/bash
eval `./child.py`

#1


17  

No process can change its parent process (or any other existing process' environment).

没有进程可以更改其父进程(或任何其他现有进程的环境)。

You can, however, create a new environment by creating a new interactive shell with the modified environment.

但是,您可以通过使用已修改的环境创建新的交互式shell来创建新环境。

You have to spawn a new copy of the shell that uses the upgraded environment and has access to the existing stdin, stdout and stderr, and does its reinitialization dance.

您必须生成一个使用升级环境的shell的新副本,并且可以访问现有的stdin,stdout和stderr,并进行重新初始化。

You need to do something like use subprocess.Popen to run /bin/bash -i.

你需要做一些事情,比如使用subprocess.Popen运行/ bin / bash -i。

So the original shell runs Python, which runs a new shell. Yes, you have a lot of processes running. No it's not too bad because the original shell and Python aren't really doing anything except waiting for the subshell to finish so they can exit cleanly, also.

所以原始的shell运行Python,它运行一个新的shell。是的,你有很多进程在运行。不,它不是太糟糕,因为原来的shell和Python并没有真正做任何事情,除了等待子shell完成,所以他们也可以干净利落地退出。

#2


13  

It's not possible, for any child process, to change the environment of the parent process. The best you can do is to output shell statements to stdout that you then source, or write it to a file that you source in the parent.

对于任何子进程,都不可能改变父进程的环境。您可以做的最好的事情是将shell语句输出到您之后获取的stdout,或将其写入您在父项中提供的文件。

#3


11  

I would use the bash eval statement, and have the python script output the shell code

我会使用bash eval语句,并让python脚本输出shell代码

child.py:

child.py:

#!/usr/bin/env python
print 'FOO="A_Value"'

parent.sh

parent.sh

#!/bin/bash
eval `./child.py`