有没有办法将环境变量传递给python脚本?
我正在尝试编写一个python测试,其中包括测试所提供的环境变量是否有效。我传递的环境变量如下有没有办法将环境变量传递给python脚本?,python,django,python-3.x,python-unittest,django-tests,Python,Django,Python 3.x,Python Unittest,Django Tests,我正在尝试编写一个python测试,其中包括测试所提供的环境变量是否有效。我传递的环境变量如下 env=EnvironmentVarGuard() GOOGLE\u凭证是从python文件导入的。 GOOGLE\u凭证在.env中工作,但一旦我从文件导入,就会收到以下错误 self.__credentials = ServiceAccountCredentials.from_json_keyfile_dict( File "/Users/esir/CFA/DebunkBot/venv/lib
env=EnvironmentVarGuard()
GOOGLE\u凭证
是从python文件导入的。
GOOGLE\u凭证
在.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
根据我的建议,我认为您应该将其作为一种工具使用,如下所示:
from test.support import EnvironmentVarGuard
with EnvironmentVarGuard() as env:
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
# put your code inside context manager
您是如何使用您的EnvironmentVarGuard
?您能否共享包含它的测试用例,包括env.set
use?@bernardoduart我已经更新了代码
from test.support import EnvironmentVarGuard
with EnvironmentVarGuard() as env:
env.clear()
env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS)
# put your code inside context manager