[英]Is there a way to pass in environment variables to python script?
I am trying to write a python test that involves testing if the environment variables provided are valid.我正在尝试编写一个 python 测试,其中涉及测试提供的环境变量是否有效。 I am passing the environment variables as follows env = EnvironmentVarGuard()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
The GOOGLE_CREDENTIALS
is an import from a python file.我按如下方式传递环境变量env = EnvironmentVarGuard()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
GOOGLE_CREDENTIALS
是从 python 文件导入的。 the GOOGLE_CREDENTIALS
works while in a .env
but once I do the import from a file I receive the following error GOOGLE_CREDENTIALS
在.env
中工作但是一旦我从文件中导入我收到以下错误
self.__credentials = ServiceAccountCredentials.from_json_keyfile_dict(
File "/Users/esir/CFA/DebunkBot/venv/lib/python3.8/site-packages/oauth2client/service_account.py", line 251, in from_json_keyfile_dict
return cls._from_parsed_json_keyfile(keyfile_dict, scopes,
File "/Users/esir/CFA/DebunkBot/venv/lib/python3.8/site-packages/oauth2client/service_account.py", line 185, in _from_parsed_json_keyfile
signer = crypt.Signer.from_string(private_key_pkcs8_pem)
File "/Users/esir/CFA/DebunkBot/venv/lib/python3.8/site-packages/oauth2client/_pure_python_crypt.py", line 167, in from_string
marker_id, key_bytes = pem.readPemBlocksFromFile(
File "/Users/esir/CFA/DebunkBot/venv/lib/python3.8/site-packages/pyasn1_modules/pem.py", line 44, in readPemBlocksFromFile
substrate = ''.encode().join([base64.b64decode(x.encode()) for x in certLines])
File "/Users/esir/CFA/DebunkBot/venv/lib/python3.8/site-packages/pyasn1_modules/pem.py", line 44, in <listcomp>
substrate = ''.encode().join([base64.b64decode(x.encode()) for x in certLines])
File "/Users/esir/.pyenv/versions/3.8.0/lib/python3.8/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
the part using the env variable is ServiceAccountCredentials.from_json_keyfile_dict(json.loads(os.getenv('GOOGLE_CREDENTIALS'), strict=False), scopes=self.__scope)
使用环境变量的部分是ServiceAccountCredentials.from_json_keyfile_dict(json.loads(os.getenv('GOOGLE_CREDENTIALS'), strict=False), scopes=self.__scope)
so my question is why does it work if I just set the GOOGLE_CREDENTIALS
on the .env
file but fails if I try using EnvironmentVarGuard
所以我的问题是为什么如果我只是在.env
文件上设置GOOGLE_CREDENTIALS
但如果我尝试使用EnvironmentVarGuard
会失败
Update Here is how I am using the EnvironmentVarGuard更新这是我使用 EnvironmentVarGuard 的方式
env = EnvironmentVarGuard()
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
As per the docs I believe you should be using it as a ContextManager as shown below:根据文档,我相信您应该将其用作ContextManager ,如下所示:
from test.support import EnvironmentVarGuard
with EnvironmentVarGuard() as env:
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
# put your code inside context manager
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.