Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Setting and retrieving environmental variables in flask applications

I want to build a very simple REST api using python3/flask.

Say for example I want to set my SECRET_KEY required by flask as env var.

What is the recommended way of going about it?

I am aware of python-dotenv package that allows (or should I say requires?) the .flaskenv file with env vars set as key-value pairs in the form of

SECRET_KEY="my_secret_key"
DB_NAME="mydatabase"

etc.

Then (I assume) I can create a settings.py file such as

import os
SECRET_KEY = os.getenv('SECRET_KEY`)

and then perform an import settings on my flask files and so on.

My main question is how can this be adapted in a containerized environment where there will not be such an .flaskenv file but the respective variables will be available as runtime env vars in the container itself (say via its orchestrator)

Will the above form of settings.py be able to retrieve env vars in the absence of .flaskenv?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

python-dotenv actually has nothing to do with Flask. It is for your .env file to be translated into actual env variables. So if you're going to have actual env variables without it, your os.getenv should still work.

Sidenote: You can also use os.environ:

os.environ.get("SECRET")
over 4 years ago · Santiago Trujillo Report

0

Set your environment variable in the interpreter:

export SECRET_KEY=123

Call the variable with environ.get():

from os import environ
from flask import Flask

app = Flask(__name__)

app.config['SECRET_KEY'] = environ.get('SECRET_KEY')

Verify:

@app.route('/verify')
def verify():
    return '<p>' + app.config['SECRET_KEY'] + '</p>'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!