尝试在以下代码中设置ENV变量
import os
dicta = {}
def setv(evar, evalue):
os.environ[evar] = evalue
dicta.setdefault('UENV', {}).update({evar: evalue})
# Set environment variables
setv('API_USER', 'username')
setv('API_PASSWORD', 'secret')
setv('NUMBER', 1)
在最后一个将NUMBER变量设置为数值1的语句上,出现以下错误:
Traceback (most recent call last):
File "./pyenv.py", line 19, in <module>
setv('NUMBER', 1)
File "./pyenv.py", line 13, in setv
os.environ[evar] = evalue
File "/home/python/3.6.3/1/el-6-x86_64/lib/python3.6/os.py", line 674, in __setitem__
value = self.encodevalue(value)
File "/home/python/3.6.3/1/el-6-x86_64/lib/python3.6/os.py", line 744, in encode
raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not int
我不想将变量值转换为str并将值保留为int。关于将NUMBER值保留为数字1的任何想法,但看不到此错误消息
答案 0 :(得分:1)
环境变量是字符串值。从环境中导入它们后,将它们类型转换回整数是一种方法。