如何将环境变量传递给我的烧瓶应用程序

时间:2018-05-30 06:56:00

标签: python flask environment-variables

我是Python新手并编写一个简单的flask api,它将连接到azure cosmos DB并返回一些响应。

我希望将db连接字符串作为环境变量传递,因为我需要将此应用程序停靠。

所以我不确定如何将此连接字符串作为环境变量传递给我的Flask应用程序,以及如何从命令窗口运行和测试我的Flask应用程序。

以下是我的代码。

import os
from flask import Flask, request, render_template
from azure.storage.table import TableService, Entity

APP = Flask(__name__)
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
connectionstring = os.environ['connectionstring']

@APP.route('/getdata')
def view_registered_guests():
print("Inside method");

table_service = TableService(connection_string=connectionstring)
table_name = 'tablebasics'
entity = table_service.get_entity(table_name, 'Harp', '2')
print(entity['email'])
print(entity['phone'])
return "Email: "+entity['email'] +" phone no: "+ entity['phone'];

if __name__ == '__main__':
APP.run(debug=True)

任何帮助将不胜感激。

谢谢,

2 个答案:

答案 0 :(得分:0)

使用os模块

os.environ["connectionstring"]

您可以使用SET

在Windows cmd中设置环境变量

set connectionstring=SOMETHING

答案 1 :(得分:0)

为了测试这个,我刚刚在系统环境变量(系统变量)中添加了“connectionstring”变量及其值,并运行了我的py文件,它运行良好。

谢谢大家的提示。

相关问题