Overview
The Muebles Roble application uses environment variables for configuration management, loaded from a.env file using python-dotenv.
Environment variables allow you to configure the application without hardcoding values, making it easy to switch between development, testing, and production environments.
Environment File Setup
Creating the .env File
- Copy the template:
- Edit
.envwith your configuration
Template File
The.env-template provides a reference:
.env-template
Configuration Variables
Flask Configuration
FLASK_APP
Specifies the application entry point for Flask CLI commands.
FLASK_ENV
Sets the Flask environment mode.Options:
development- Enable debug mode, auto-reloadproduction- Disable debug, optimize for performance
Database Configuration
DB_USER
MySQL database username.
DB_PASSWORD
MySQL database password.
DB_HOST
MySQL server hostname or IP address.
DB_PORT
MySQL server port.
DB_NAME
MySQL database name.
Security Configuration
SECRET_KEY
Secret key for session management and CSRF protection.Critical for:
- Session encryption
- CSRF token generation
- Cookie signing
Generating a Secure SECRET_KEY
Generate a cryptographically secure key:Configuration Loading
How Config is Loaded
The application loads configuration inconfig.py:
config.py
Loading Order
Environment-Specific Configurations
Development Environment
.env (development)
Production Environment
.env (production)
Database URI Construction
The database URI is automatically constructed from environment variables:Example URIs
SQLAlchemy Configuration
SQLALCHEMY_DATABASE_URI
Automatically built from database environment variables (see above).SQLALCHEMY_TRACK_MODIFICATIONS
Accessing Environment Variables
In Python Code
In Application Config
Variables are accessed through theConfig class:
Environment Variable Best Practices
Never commit .env files
Never commit .env files
Always add
.env to .gitignore:.gitignore
Use .env-template for documentation
Use .env-template for documentation
Keep
.env-template updated with all required variables:.env-template
Use strong secrets in production
Use strong secrets in production
Generate cryptographically secure keys:
Validate required variables
Validate required variables
Check for required variables at startup:
Document all variables
Document all variables
Comment your
.env-template file:Troubleshooting
Common Issues
SECRET_KEY must be set error
SECRET_KEY must be set error
Error:
ValueError: SECRET_KEY must be set in production environmentSolution: Add SECRET_KEY to your .env file:Database connection failed
Database connection failed
Error: Can’t connect to MySQL serverCheck:
- Verify
DB_HOST,DB_PORT,DB_USER,DB_PASSWORDin.env - Ensure MySQL is running:
sudo systemctl status mysql - Test connection:
mysql -h $DB_HOST -u $DB_USER -p - Check firewall rules
Environment variables not loading
Environment variables not loading
Problem: Variables from
.env not availableSolutions:- Ensure
.envfile is in project root - Check file is named exactly
.env(not.env.txt) - Verify
load_dotenv()is called inconfig.py - Restart the application
Wrong database selected
Wrong database selected
Problem: Using wrong database (dev vs prod)Solution: Check
FLASK_ENV and DB_NAME:Quick Reference
Required Variables
| Variable | Required | Default | Description |
|---|---|---|---|
DB_USER | Yes | - | Database username |
DB_PASSWORD | Yes | - | Database password |
DB_HOST | Yes | - | Database host |
DB_PORT | Yes | - | Database port |
DB_NAME | Yes | - | Database name |
SECRET_KEY | Prod | dev-key | Secret key for sessions |
FLASK_APP | No | app.py | Application entry point |
FLASK_ENV | No | development | Environment mode |
Example .env File
.env
Next Steps
Setup Guide
Complete environment setup instructions
Deployment
Deploy to production
Project Structure
Understand the application architecture
Coding Conventions
Follow best practices