有没有办法将环境变量传递给 python 脚本?
Is there a way to pass in environment variables to python script?
我正在尝试编写一个 python 测试,其中涉及测试所提供的环境变量是否有效。我按如下方式传递环境变量
env = EnvironmentVarGuard()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
GOOGLE_CREDENTIALS
是从 python 文件导入的。
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
使用环境变量的部分是
ServiceAccountCredentials.from_json_keyfile_dict(json.loads(os.getenv('GOOGLE_CREDENTIALS'), strict=False), scopes=self.__scope)
所以我的问题是为什么如果我只在 .env
文件上设置 GOOGLE_CREDENTIALS
就可以工作,但如果我尝试使用 EnvironmentVarGuard
就会失败
更新 这是我使用 EnvironmentVarGuard
的方式
env = EnvironmentVarGuard()
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
根据docs I believe you should be using it as a ContextManager如下图:
from test.support import EnvironmentVarGuard
with EnvironmentVarGuard() as env:
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
# put your code inside context manager
我正在尝试编写一个 python 测试,其中涉及测试所提供的环境变量是否有效。我按如下方式传递环境变量
env = EnvironmentVarGuard()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
GOOGLE_CREDENTIALS
是从 python 文件导入的。
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
使用环境变量的部分是
ServiceAccountCredentials.from_json_keyfile_dict(json.loads(os.getenv('GOOGLE_CREDENTIALS'), strict=False), scopes=self.__scope)
所以我的问题是为什么如果我只在 .env
文件上设置 GOOGLE_CREDENTIALS
就可以工作,但如果我尝试使用 EnvironmentVarGuard
更新 这是我使用 EnvironmentVarGuard
的方式env = EnvironmentVarGuard()
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
根据docs I believe you should be using it as a ContextManager如下图:
from test.support import EnvironmentVarGuard
with EnvironmentVarGuard() as env:
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
# put your code inside context manager