如何在python中更改环境变量? [英] How to change environment variables in python?

查看:147
本文介绍了如何在python中更改环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的python脚本(test.py):

i have a simple python script ( test.py ):

import cx_Oracle
from cx_Oracle
tns = cx_Oracle.makedsn('10.10.1.3', 1521, 'etst')
db = cx_Oracle.connect('test', 'test', tns)

如果我使用环境设置运行脚本,这是可行的:

it is work if I run script with enviroument settings:

export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
./test.py

我可以在python脚本中设置环境变量吗?

Can I set environment variables in python script ?

os.environ['LD_LIBRARY_PATH'] = "/usr/lib/oracle/11.2/client64/lib"
os.putenv('LD_LIBRARY_PATH', "/usr/lib/oracle/11.2/client64/lib/")

不起作用。

推荐答案

您可以设置这样,但是 $ LD_LIBRARY_PATH 会被之前已经运行的加载器读取,因此您必须设置一个预先在外部。

You can set them that way, however $LD_LIBRARY_PATH is read by the loader which has already run before that therefore you must set that one externally beforehand.

这篇关于如何在python中更改环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆