当前位置: 首页 > news >正文

4: Accessing Environment Variables(Working with programs)

2019独角兽企业重金招聘Python工程师标准>>> 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 os
print(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

相关文章:

  • flat network 原理与配置 - 每天5分钟玩转 OpenStack(86)
  • 4 张 GIF 图帮助你理解二叉查找树
  • linux目录结构详细介绍
  • Android EditText光标位置(定位到最后)
  • 深刻理解Python中的元类(metaclass)
  • c++中类对象的内存对齐
  • Significance Testing
  • 延迟脚本的方式
  • shell中变量的查看和删除
  • 算法分析-分治 归并排序,递归插入排序,二分查找
  • mysql搭建及数据迁移教程
  • mysql 5.7 zip 文件在 windows下的安装
  • linux_group总结
  • scrapy 学习笔记
  • 关于 jquery 选择器的 深入理解 -1
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 2019年如何成为全栈工程师?
  • angular2 简述
  • EOS是什么
  • iOS 颜色设置看我就够了
  • Java面向对象及其三大特征
  • Laravel 实践之路: 数据库迁移与数据填充
  • Odoo domain写法及运用
  • PAT A1017 优先队列
  • Spark RDD学习: aggregate函数
  • spring学习第二天
  • SQL 难点解决:记录的引用
  • text-decoration与color属性
  • Vue2.0 实现互斥
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 那些年我们用过的显示性能指标
  • 算法-插入排序
  • 小试R空间处理新库sf
  • 用mpvue开发微信小程序
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​iOS实时查看App运行日志
  • #Linux(Source Insight安装及工程建立)
  • #pragma multi_compile #pragma shader_feature
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • ${ }的特别功能
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (java)关于Thread的挂起和恢复
  • (离散数学)逻辑连接词
  • (力扣题库)跳跃游戏II(c++)
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (转)IOS中获取各种文件的目录路径的方法
  • (转)iOS字体
  • (转)linux下的时间函数使用
  • .net Stream篇(六)
  • .net 简单实现MD5
  • .NET下的多线程编程—1-线程机制概述
  • .NET中统一的存储过程调用方法(收藏)
  • :“Failed to access IIS metabase”解决方法
  • @Not - Empty-Null-Blank