博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4: Accessing Environment Variables(Working with programs)
阅读量:6511 次
发布时间:2019-06-24

本文共 1266 字,大约阅读时间需要 4 分钟。

hot3.png

We can run many programs from bash, including Python. To run the Python interpreter from the bash shell, just type python at the command prompt.

Once inside the command prompt, you can access environment variables like this:

 

import os

print(os.environ["FOOD"])

The os package is built into the Python standard library, and contains many useful functions for working with the operating system.

os.environ is a dictionary containing all of the values of environment variables. You can access any environment variable by specifying it as a key, just like with any Python dictionary.

This shows you a hint of the power of environment variables -- we can use them to set configuration in Python scripts and in other places. This is useful when configuration is secret (like access keys), or is changing quickly.

Instructions

Type python to fire up the Python interpreter.

  • In the interpreter, you can run Python commands by typing them in and hitting enter at the end of each line.

  • Run the following code in the interpreter:

import osprint(os.environ["FOOD"])
  • When you're done, type exit() to exit the interpreter.

Finally, type echo $FOOD to verify that the value of the FOOD variable is the same in Python and in the shell.

转载于:https://my.oschina.net/Bettyty/blog/746505

你可能感兴趣的文章
jre与jdk的区别
查看>>
全景图的种类
查看>>
git 维护
查看>>
jfinal框架下使用c3P0连接池连接sql server 2008
查看>>
struts2中使用标签操作静态方法等
查看>>
熬夜写了一个小游戏,向SpaceX聊表敬意
查看>>
apache 开启 gzip 压缩服务
查看>>
开源 免费 java CMS - FreeCMS1.5-建站向导
查看>>
jquery 1.6以上版本 全选
查看>>
AppCan 学习
查看>>
flask框架
查看>>
《疯狂Java讲义》学习笔记(十)异常处理
查看>>
ELK 5.x日志分析 (二) Elasticserach 5.2 安装
查看>>
一次奇怪的AP注册异常问题处理
查看>>
TableStore: 海量结构化数据分层存储方案
查看>>
Unity 4.x游戏开发技巧集锦(内部资料)
查看>>
自适应网页设计
查看>>
java SpringUtil获取bean
查看>>
赛门铁克开启“容灾即服务”时代
查看>>
复杂度归纳--小结
查看>>