Python |Environment Variables

在这里插入图片描述
在这里插入图片描述
Understanding and being able to change environment variables can be really useful to quickly alter a program’s behavior. Usually, we can do this by just making some minor changes in the environment the programs are running in. From a command line prompt, we can check these variables using the env or nth command.
在这里插入图片描述
在这里插入图片描述
That’s a lot of different variables, but what are they for? It all depends on the variable itself. Some are more important than others. For example, the path variable is a very important one. Let’s print out the contents of just that one using the echo command.
在这里插入图片描述
Echo is a command that we use to print texts and Linux shell, and when we want to access the value of the variable in the shell, we need a prefix and name of the variable with a dollar sign. Here, we’ve printed the contents of the path variable. The shell uses this environment variable to figure out where to look for executable files, and we call them while specifying a directory. All those directories listed there are where the shell will look for programs. For example, when we call the Python 3 program, the shell checks each of the directories listed in the path variable in order, and when it finds a program called Python 3, it executes it.

We can read the contents of these variables from Python. Let’s use a Python script to check that out.
在这里插入图片描述
To access environment variables, we use the Environ dictionary provided by the OS module. In this case, we’re using a dictionary method that we haven’t used before. The getMethod is a bit similar to how we’ve been accessing dictionary values up until now. The difference is what happens when the value isn’t present. When we retrieve a value from a dictionary using the key as in OS.environ[fruit] and the key isn’t present, we get an error. If we use a getMethod instead, we can specify what value should be returned if the key isn’t present. In other words, the getMethod allows us to specify a default value when the key that we’re looking for isn’t in the dictionary. So what we’re asking Python to do is try to retrieve the value associated with the key, but if the key’s not defined return an empty string instead. We’re doing this for three different variables; home, shell, and fruit. Let’s run the script and see what happens.
在这里插入图片描述
We got the values for home and shell, but not for fruit. Well, that’s because that variable isn’t defined in the current environment. To define it in a way that our script we’ll be able to see it, we need to run this in our command-line.

So we define the variable by just setting a value using the equal sign and leaving no spaces in between. Along with this, the export keyword tells a shell that we want the value we set to be seen by any commands that we call. Now, let’s call our script again.
在这里插入图片描述
Cool, this time we got the value that we wanted. So you now know how to get the value of any environment variables from your Python scripts. Coming up, we’ll dive into how command lines tell us whether they’ve succeeded or failed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Crypto投资小白书

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值