os.getenv()没有提取我先前声明的变量

时间:2018-06-23 15:43:17

标签: python

我是python的新手,我似乎无法弄清楚为什么我无法使os.getenv()函数正常工作。如果不存在某些内容,它将继续返回默认值None。这是我的代码:

import os
import datetime
import plaid
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify

app = Flask(__name__)

PLAID_CLIENT_ID='entercharhere'
PLAID_ENV='sandbox'

#edit here
os.environ['PLAID_CLIENT_ID']='entercharhere'
os.environ['PLAID_ENV']='sandbox'

print(PLAID_CLIENT_ID)

# Fill in your Plaid API keys - https://dashboard.plaid.com/account/keys
PLAID_CLIENT_ID = os.getenv('PLAID_CLIENT_ID')
# Use 'sandbox' to test with Plaid's Sandbox environment (username: user_good,
# password: pass_good)
# Use `development` to test with live users and credentials and `production`
# to go live
PLAID_ENV = os.getenv('PLAID_ENV','sandbox')

print(PLAID_CLIENT_ID)

我收到的输出是entercharhereNone

编辑

我结束了使用以下命令切换变量声明以将其发送到环境的方法:

os.environ['PLAID_CLIENT_ID']='entercharhere'
os.environ['PLAID_ENV']='sandbox'

这是最好的方法吗?

1 个答案:

答案 0 :(得分:-1)

完美运行:

import os

os.environ['SOMEVAR'] =  'bla'

print(os.getenv('SOMEVAR'))

# in your shell: export SOMEVAR1="blabla"
print(os.getenv('SOMEVAR1'))

Gives:
bla
blabla