简体   繁体   English

如何使用python 2.7从特定文件中读取环境变量

[英]How to read environment variables from specific file with python 2.7

In order to declare a number of environment variables then call some python scripts using them, I create a myfile.sh file which is the on to run by bash myfile.sh .为了声明一些环境变量然后使用它们调用一些 python 脚本,我创建了一个 myfile.sh 文件,它是由 bash myfile.sh 运行的。 I have, however, plenty of scripts that should read these environment variables, but cannot create a myfile.sh for each one!但是,我有很多脚本应该读取这些环境变量,但无法为每个脚本创建一个 myfile.sh!

So my idea is to create an environment variable file and access it by each of my python scripts.所以我的想法是创建一个环境变量文件并通过我的每个 python 脚本访问它。

So my question is, how to access such a file with python 2.7 ?所以我的问题是,如何使用 python 2.7 访问这样的文件?

A most relevant question is the following: Where does os.environ read the environment variables from?一个最相关的问题如下: os.environ 从哪里读取环境变量?

It should be noted that I cannot install additional libraries such as dotenv.需要注意的是,我无法安装额外的库,例如 dotenv。 So a solution, if possible, should be based on standard libraries.因此,如果可能,解决方案应该基于标准库。

Any help would be mostly appreciated!任何帮助将不胜感激!

I have to create a new environment variables file that should be accessed by my script.我必须创建一个应该由我的脚本访问的新环境变量文件。

That's not how it works.这不是它的工作原理。 What you need is to set the environment variables from the file BEFORE calling your script:您需要的是在调用脚本之前从文件中设置环境变量:

myenv.sh我的环境文件

export DB_USER="foo"
export DB_PWD="bar"
export DB_NAME="mydb"
# etc

myscript.py脚本文件

import os

DB_USER = os.getenv("DB_USER")
DB_PWD = os.getenv("DB_PWD")
DB_NAME = os.getenv("DB_NAME")

# etc

Then然后

$ source /path/to/myenv.sh
$ python /path/to/myscript.py

Most often you will want to wrap the above two lines in a shell script to make your sysadmin's life easier.大多数情况下,您希望将以上两行代码包含在一个 shell 脚本中,以使系统管理员的工作更轻松。

EDIT: if you want those env vars to be "automagically" set, you can always source them from .bashrc or some similar place - but then this is a sysadmin question, not a programming one.编辑:如果您希望这些 env vars 被“自动”设置,您总是可以从 .bashrc 或类似的地方获取它们 - 但这是一个系统管理员问题,而不是一个编程问题。

Now the question is: do you really need to use environment variables here ?现在的问题是:你真的需要在这里使用环境变量吗? You can as well use a python settings file - a plain Python module that just defines those variables, ie:您也可以使用 python 设置文件 - 一个只定义这些变量的普通 Python 模块,即:

mysettings.py我的设置.py

DB_USER = "foo"
DB_PWD = "bar"
# etc

and make sure the path to the directory containing this script is in your $PYTHONPATH.并确保包含此脚本的目录的路径在您的 $PYTHONPATH 中。 Then your scripts only have to import it (like they import any other module), and you're done.然后你的脚本只需要导入它(就像它们导入任何其他模块一样),你就完成了。

Icing on the cake: you can even mix both solutions, by having your settings module looking up environment variables and providing a default, ie:锦上添花:您甚至可以混合两种解决方案,让您的设置模块查找环境变量并提供默认值,即:

mysettings.py我的设置.py

import os
DB_USER = os.getenv("DB_USER", "foo")
DB_PWD = os.getenv("DB_PWD", "bar")
# etc

Show environment from commandline从命令行显示环境

  • linux: export linux: export
  • windows: set窗户: set

Setting an environment variable设置环境变量

  • linux: export foo=bar linux: export foo=bar
  • windows: set foo=bar窗口: set foo=bar

Printing env var:打印环境变量:

  • linux: echo $foo linux: echo $foo
  • windows: echo %foo%窗口: echo %foo%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM