python shell 继承 环境变量,如何在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

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?

解决方案

This isn't possible.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值