def resolve_dotenv_file(path, stage=None):
'''
Resolve dotenv file and load environment vars if it exists.
If stage parameter is provided, then stage specific .env file is resolved,
for instance .env.production if stage=production etc.
If stage is None, just .env file is resolved.
'''
filename = '.env' + ('' if not stage else '.{}'.format(stage))
dotenv_path = os.path.join(path, filename)
fallback_path = os.path.join(path, '.env')
if fs.exists(dotenv_path):
info('Resolving env file: {}'.format(cyan(dotenv_path)))
dotenv.load_dotenv(dotenv_path)
elif fs.exists(fallback_path):
info('Resolving env file: {}'.format(cyan(fallback_path)))
dotenv.load_dotenv(fallback_path)
python类load_dotenv()的实例源码
def __init__(self, configfile='.env'):
'''Initialize.
@param: configfile a .env style config file. See README for more.
'''
if os.path.exists(configfile):
# we set ourselves as load_dotenv has system env variables to take
# precedence which in our experience is confusing as a user changes a
# var and re-runs and nothing happens
# dotenv.load_dotenv('.env')
out = dotenv.main.dotenv_values(configfile)
# we need stuff in the environment for docker
os.environ.update(out)
self.config = os.environ
rds_uri = self.config.get('RDS_URI')
if not rds_uri:
print('Warning: RDS_URI is not set. please set, or run `python main.py rds`')
def load_settings_from_env(root_path):
dotenv.load_dotenv(os.path.join(root_path, '.env'))
def load_dot_env():
if not PIPENV_DONT_LOAD_ENV:
# If the project doesn't exist yet, check current directory for a .env file
project_directory = project.project_directory or '.'
denv = dotenv.find_dotenv(PIPENV_DOTENV_LOCATION or os.sep.join([project_directory, '.env']))
if os.path.isfile(denv):
click.echo(crayons.normal('Loading .env environment variables…', bold=True), err=True)
dotenv.load_dotenv(denv, override=True)
def _maybe_load(env):
if os.path.isfile(env):
logger.debug("Loading .env file %s" % env)
load_dotenv(env)
else:
logger.info(".env file %s does not exist, not loading." % env)