为什么要将settings.py中的os.getenv()用于Python Flask应用程序?

为什么要将settings.py中的os.getenv()用于Python Flask应用程序?,python,flask,Python,Flask,我分叉了这个应用程序: 您可以在settings.py和下面看到它使用DEBUG=os.getenv'CNF_DEBUG','True'='True' 我在终端中激活了我的conda env,并在激活iPython控制台后输入命令: 'CNF_DEBUG' in os.environ 这是错误的 settings.py的其余部分如下所示: import os import datetime from urllib.parse import urljoin APP_NAME = "c

我分叉了这个应用程序:

您可以在settings.py和下面看到它使用DEBUG=os.getenv'CNF_DEBUG','True'='True' 我在终端中激活了我的conda env,并在激活iPython控制台后输入命令:

'CNF_DEBUG' in os.environ
这是错误的

settings.py的其余部分如下所示:

import os
import datetime
from urllib.parse import urljoin

APP_NAME = "cnf"
APP_SYSTEM_ERROR_SUBJECT_LINE = APP_NAME + "system error"

#Flask Settings
CSRF_ENABLES = True

ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# looks like a trick to get it to select default True == True
DEBUG = (os.getenv('CNF_DEBUG', 'True') == 'True')
TESTING = (os.getenv('CNF_TESTING', 'True') == 'True')
FLASK_DEBUG = DEBUG
FLASK_BIND = os.getenv('CNF_BIND', 'localhost')
FLASK_PORT = int(os.getenv('CNF_PORT', 8888))
ROOT_URL = 'http://' + FLASK_BIND + ':' + str(FLASK_PORT) + '/'

TEMPLATE_FOLDER = os.path.join(ROOT_PATH, 'cnf', 'templates')
STATIC_FOLDER = os.path.join(ROOT_PATH, 'cnf', 'static')

MONGODB_HOST = os.getenv('CNF_MONGO_HOST', 'localhost')
MONGODB_PORT = int(os.getenv('CNF_MONGO_PORT', 27017))
MONGODB_DB = os.getenv('CNF_MONGO_DB', 'cnf') # name of db

#encryption, cryptographically signs client-side cookies so they cannot be tampered with
#if tampered, session becomes invalid, insecure not to be used production
SECRET_KEY = 'this key is not secure so be careful until it is'

# Flask-User Settings
# from https://flask-user.readthedocs.io/en/latest/configuring_settings.html
USER_APP_NAME = "cnf"
USER_ENABLE_EMAIL = False  # register with email
USER_ENABLE_CONFIRM_EMAIL = False # force users to confirm email
USER_ENABLE_USERNAME = True  # enable username authentication
USER_REQUIRE_RETYPE_PASSWORD = False  # simplify register form
USER_EMAIL_SENDER_NAME = 'nobu'
#USER_EMAIL_SENDER_EMAIL = 'nobu.kim66@gmail.com' # set up Flask Mail for this
USER_ENABLE_CHANGE_USERNAME = True
USER_ENABLE_CHANGE_PASSWORD = True
USER_ENABLE_FORGOT_PASSWORD = True
#USER_ENABLE_REGISTER = True
USER_ENABLE_REGISTRATION = True
USER_ENABLE_REMEMBER_ME = True
USER_AFTER_LOGIN_ENDPOINT = 'main.member_page'
USER_AFTER_LOGOUT_ENDPOINT = 'main.home_page'


# Flask-Mail settings
# For smtp.gmail.com to work, you MUST set "Allow less secure apps" to ON in Google Accounts.
# Change it in https://myaccount.google.com/security#connectedapps (near the bottom).
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_USE_SSL = False
MAIL_USE_TLS = True
MAIL_USERNAME = 'nobu.kim66@gmail.com'
MAIL_PASSWORD = "..."
#ADMINS = ['"Admin One" <admin@gmail.com>,]

# Sendgrid settings
#SENDGRID_API_KEY='place-your-sendgrid-api-key-here'
正如上面的评论所指出的,我认为这可能只是一个技巧,而不是使用评论,但是这样做还有什么其他原因呢?我在哪里可以找到CNF_测试和CNF_调试?
它还为根URL的FLASK\u绑定和FLASK\u端口执行此操作。

这些是环境变量

编写env变量有许多优点,其中包括更容易的容器设置、更容易和标准化地访问配置(甚至在程序之间)以及更好的可伸缩性


有关环境变量的更多信息,请参阅or per@flakes建议。

使用环境配置部署类型是良好设计的一部分,正如12-Factor应用程序中所述,您似乎发布了敏感/私人信息。请重置您的密码和/或撤销API密钥和令牌,因为它们在发布到internet上时被视为已被泄露。感谢您的提示