调用python时设置$ ENV {variable} [英] setting $ENV{variable} when calling python

查看:328
本文介绍了调用python时设置$ ENV {variable}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,想要重现Perl工作时使用的方便。
调用Perl脚本时,我通常设置一些$ ENV变量(如VERBOSE,DEVELOP和DEBUG)。在被叫脚本中,我使用

 我的$ verbose = $ ENV {VERBOSE}恢复其值
我的$ develop = $ ENV {DEVELOP};
我的$ debug = $ ENV {DEBUG};

这允许以这些变量为条件的打印stmts。



我可以在Python中做同样的事情吗?感谢先前的回复(谢谢!),使用脚本中的os.environ [var]来访问这些值。但是,当我从命令行调用脚本时,我无法弄清楚如何为一个变量分配一个值,当callinbg是一个Perl脚本



可以在调用python脚本的命令行上为这些变量设置值?



TIA

解决方案

他们可以通过 os .environ 字典。

  os.environ ['VERBOSE'] 


I'm new to Python and would like reproduce a convenience I used when working in Perl. When calling a Perl script I usually set some $ENV variables (like VERBOSE, DEVELOP and DEBUG). Inside the called script I recover their values using

my $verbose=$ENV{VERBOSE};
my $develop=$ENV{DEVELOP};
my $debug=$ENV{DEBUG};

This allow print stmts conditional on these variables.

Can I do the same thing in Python? I know thanks to previous responses (Thank you!) to use os.environ[var] within the script to access the values. But I have not been able to figure out how to assign a value to a variable when I call the script from the command line as I could when callinbg a Perl script

Can values be set for such variables on the commandline invoking a python script?

TIA

解决方案

They are accessible via the os.environ dictionary.

os.environ['VERBOSE']

这篇关于调用python时设置$ ENV {variable}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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