python中环境变量相关os.putenv函数使用注意事项

先说结论:
如果期望设置的环境变量立刻生效,也就收从设置开始后面的运行部分都能获取到该变量,直接使用这种方式os.environ[‘myenv’] = ‘prod’, 使用os.putenv方式不行,因为os.putenv只能在subprocesses started with os.system(), popen() or fork() and execv()中生效

官方文档:
https://docs.python.org/2/library/os.html#os.environ
Note Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.

代码

import os


def print_all():
    print("os.environ['myenv']=%s" % os.environ.get('myenv'))
    print("os.getenv('myenv')=%s" % os.getenv('myenv'))
    os.system('env | grep myenv')
    print("end of print all")


def delete_myenv():
    if 'myenv' in os.environ:
        print("get myenv in os.environ, pop it")
        os.environ.pop('myenv')
    else:
        print("no myenv in os.environ")

    if os.getenv('myenv'):
        print("get myenv in os.getenv, unset it")
        os.unsetenv('myenv')
    else:
        print("no myenv in os.getenv")

    print("end of delete_myenv")


print("call print all env ()")
print_all()
print("---")

print("set myEnv=dev by os.putenv")
# 这种方式设置的环境变量会在后续的os.environ、os.getenv('myenv')不生效,在env命令生效
# 官方文档中写的Note Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.
# s.putenv(varname, value)
# Set the environment variable named varname to the string value. Such changes to the environment affect subprocesses started with os.system(), popen() or fork() and execv().
#
# Availability: most flavors of Unix, Windows.
#
# Note On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks. Refer to the system documentation for putenv.
# When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ.


os.putenv('myenv', 'dev')
print("call delete_myenv()")
delete_myenv()
print("call print all env ()")
print_all()

print("set myEnv=staging by os.putenv")
os.putenv('myenv', 'staging')
print("call delete_myenv()")
delete_myenv()
print("call print all env ()")
print_all()

print("set myEnv=staging by os.environ[x]=y")
# 这种方式设置的环境变量会在后续的os.environ、os.getenv('myenv'),env命令都生效
os.environ['myenv'] = 'prod'
print("call print all env ()")
print_all()

效果

call print all env ()
os.environ['myenv']=None
os.getenv('myenv')=None
end of print all
#一开始本机中没有myenv这个环境变量,因此os.getenv os.environ都获取不到
---
set myEnv=dev by os.putenv
call delete_myenv()
no myenv in os.environ
no myenv in os.getenv
end of delete_myenv
call print all env ()
os.environ['myenv']=None
os.getenv('myenv')=None
myenv=dev
end of print all
# 通过os.putenv设置的环境变量,在os.getenv os.environ都获取不到,但是os.system中执行命令是可以获取到的

set myEnv=staging by os.putenv
call delete_myenv()
no myenv in os.environ
no myenv in os.getenv
end of delete_myenv
call print all env ()
os.environ['myenv']=None
os.getenv('myenv')=None
myenv=staging
end of print all

set myEnv=staging by os.environ[x]=y
call print all env ()
os.environ['myenv']=prod
os.getenv('myenv')=prod
myenv=prod
end of print all
# 通过os.environ[x]=y 方式设置的环境变量在os.getenv os.environ,以及os.system中都是可以获取到的
Process finished with exit code 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值