如何在Python中设置父shell的环境变量? [英] How to set environment variables of parent shell in Python?
问题描述
我正在尝试从Python内部修改父外壳上的环境变量.到目前为止,我一直没有尝试:
I'm trying to modify the environment variables on a parent shell from within Python. What I've attempted so far hasn't worked:
~ $ export TESTING=test
~ $ echo $TESTING
test
~ $
~ $
~ $ python
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['TESTING']
'test'
>>> os.environ['TESTING'] = 'changed'
>>> os.environ['TESTING']
'changed'
>>> quit()
~ $
~ $
~ $ echo $TESTING
test
这就是我所能想到的.能做到吗如何在Python中设置父级外壳的环境变量?
That's all I've been able to come up with. Can it be done? How do I set environment variables of parent shell in Python?
推荐答案
这是不可能的.
子进程从其父级继承其环境,而不是共享它们.因此,您对环境所做的任何修改将仅在子(python)进程中反映出来.实际上,您只是覆盖os
模块根据您的shell环境而不是shell的实际环境变量创建的字典.
Child processes inherit their environments from their parents rather than share them. Therefore any modifications you make to your environment will be reflected only in the child (python) process. Practically, you're just overwriting the dictionary the os
module has created based on your environment of your shell, not the actual environment variables of your shell.
https://askubuntu.com/questions/389683/how-we-can-change-linux-environment-variable-in-python
这篇关于如何在Python中设置父shell的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!