A Flask application can be as simple as a single file, but as a project gets bigger, it becomes overwhelming to keep all the code in one file. Python projects use packages to organize code into multiple modules.
Create Project Directory
Create the project directory
Create a project directory and enter it:mkdir flask-tutorial
cd flask-tutorial
Set up virtual environment
Follow the installation instructions to set up a Python virtual environment and install Flask for your project.python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install flask
Project Structure
The project directory will contain:
flaskr/ - A Python package containing your application code and files
tests/ - A directory containing test modules
.venv/ - A Python virtual environment where Flask and other dependencies are installed
- Installation files telling Python how to install your project
- Version control config, such as git
Simple vs Structured Application
A minimal Flask application can be a single file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
However, for larger projects, we’ll use a structured package-based approach.
Final Directory Layout
By the end of the tutorial, your project layout will look like this:
/home/user/Projects/flask-tutorial
├── flaskr/
│ ├── __init__.py
│ ├── db.py
│ ├── schema.sql
│ ├── auth.py
│ ├── blog.py
│ ├── templates/
│ │ ├── base.html
│ │ ├── auth/
│ │ │ ├── login.html
│ │ │ └── register.html
│ │ └── blog/
│ │ ├── create.html
│ │ ├── index.html
│ │ └── update.html
│ └── static/
│ └── style.css
├── tests/
│ ├── conftest.py
│ ├── data.sql
│ ├── test_factory.py
│ ├── test_db.py
│ ├── test_auth.py
│ └── test_blog.py
├── .venv/
├── pyproject.toml
└── MANIFEST.in
Version Control
If you’re using version control, the following files should be ignored:
.venv/
*.pyc
__pycache__/
instance/
.pytest_cache/
.coverage
htmlcov/
dist/
build/
*.egg-info/
You should make a habit of using version control for all your projects, no matter the size.
Next Steps
Now that the project structure is set up, you can start building the application factory.