[英]Use Config File or CLI Args for External Parameters
I am working on a Python project where the person who runs the program needs to specify some variables which then the program will use during execution. 我正在一个Python项目中,运行程序的人需要指定一些变量,程序在执行过程中将使用这些变量。 I would like to know to maintain production level coding standards what the preferred method is, use CLI or a JSONconfig file.
我想知道要维持生产级编码标准的首选方法,请使用CLI或JSONconfig文件。
PS PS
To be more specific this is a Spark job and a developer who runs the job needs to specify these external parameters 更具体地说,这是一个Spark作业,运行该作业的开发人员需要指定这些外部参数
A good method is to use a config.py file along with a .env file. 一个好的方法是将config.py文件与.env文件一起使用。 This allows you to define variables psuedo-secretly in th
.env
file, and in config.py
access them using python's dotenv
and os
libraries. 这使您可以在
.env
文件中秘密定义变量.env
,并在config.py
使用python的dotenv
和os
库访问它们。 The actual contents of the variables defined in .env
will not be exposed in the project. .env
定义的变量的实际内容不会在项目中公开。 For example, in the project directory you could have a file named .env
containing lines like 例如,在项目目录中,您可以有一个名为
.env
的文件, .env
包含如下行
# project/.env
foo='bar'
a=10
important_var='345v4325'
and in config.py
, 并在
config.py
,
# project/config.py
import os
from dotenv import load_dotenv
load_dotenv()
secret_foo = os.environ.get('foo')
secret_a = os.environ.get('a')
secret_important_var = os.environ.get('important_var')
And then import config and use the vars like so 然后导入配置并像这样使用vars
# project/module.py
import config
foo = config.secret_foo
So the project manager can modify things in .env
without having to modify any code. 因此,项目经理可以修改
.env
而无需修改任何代码。 If privacy is not an issue, you can skip the .env
stuff and define variables directly in config.py
如果隐私不是问题,则可以跳过
.env
内容,直接在config.py
定义变量
Notes 笔记
python-dotenv
is not a standard python library. python-dotenv
不是标准的python库。 If you want to avoid new dependencies, similar functionality can be achieved by defining the variables in a bash script such as 如果要避免新的依赖关系,可以通过在bash脚本中定义变量来实现类似的功能,例如
# ~/.bashrc
export foo='bar'
# and so on...
and running source ~/.bashrc
, then in config.py
remove the from dotenv import load_dotenv
and load_dotenv
lines. 并运行
source ~/.bashrc
,然后在config.py
中from dotenv import load_dotenv
和load_dotenv
行中删除。
This works well if the code is being executed on a dedicated server where you can define you environment variables how you'd like. 如果代码在专用服务器上执行,则效果很好,您可以在其中定义所需的环境变量。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--someoption", type=str, help="something")
args = parser.parse_args()
print(args.someoption)
You can use the argparse package to pass the options in the CLI, pretty simple. 您可以使用argparse软件包在CLI中传递选项,非常简单。 You can read the document here
您可以在这里阅读文档
output : 输出:
vignesh@DESKTOP-C9L0NQI:/mnt/c/Users/vprabhakaran/Downloads/sunm$ ./argprs.py --someoption printthis
printthis
To parse a config file, you can use configparser package, very easy. 要解析配置文件,可以使用configparser包,非常简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.