Revision as of 23:09, 28 July 2025 by KazVee (talk | contribs) (Add missing letter 'd' from 'need', capitalize 'Python' and 'Variables' in header, and add 'Category:Tutorials' tag to bottom of page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Configuring Environment

Python

For Python, Flask, and Django, place your environment variables in a ".env" file located in your app's root directory. This is the same directory that contains your ".htaccess" file. The web server will restrict remote access to this file.

This file should be a list of key/value pairs similar to:

DEV_ENV=development
SECRET_API_KEY=***my_secret_key***

You can add the following code to your ".htaccess" file as an extra explicit instruction for Apache to restrict access

#hide .env files from http access
<FilesMatch "\.env">
    Order allow,deny
    Deny from all
</FilesMatch>

Accessing the Variables

Python and Flask

To access your environment variables from Python you will need to load them first using the dotenv package and the operating system environment with os.

from dotenv import load_dotenv
from os import environ

You will need to give load_dotenv the path to your ".env" file and then access the variables.

#replace MY_USER_ID and MY_APP_DIR with your account root and app directory
load_dotenv("/home/MY_USER_ID.helioho.st/MY_APP_DIR/.env")

my_dev_environment = os.environ.get("DEV_ENV") # "development"

This page was last edited on 28 July 2025, at 23:09.