Flask Local Development Guide

Run lightweight Python web applications and APIs using Flask on localhost:5000.


Flask is a lightweight web framework for Python, suitable for APIs, prototyping, and small sites. The built-in development server defaults to http://localhost:5000.

Quick Start

python -m venv .venv
source .venv/bin/activate
pip install flask

app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello from Flask on localhost'

if __name__ == '__main__':
    app.run(debug=True)
python app.py

Access http://localhost:5000.

Change Port

app.run(host='0.0.0.0', port=8080, debug=True)

Or use environment variables / Flask CLI:

flask --app app run --port 8000

Comparison with Django / FastAPI

FrameworkDefault PortFeatures
Flask5000Microframework, flexible
Django8000Full-stack, Admin
FastAPI8000Asynchronous API, OpenAPI

Production Notes

The built-in server is for development only; for production, use Gunicorn/uWSGI + Nginx (see our gunicorn article).

Frequently Asked Questions

Port 5000 Conflict on macOS
macOS AirPlay Receiver may occupy port 5000; change port=5001 or disable AirPlay receiving.

Debug Mode
debug=True enables hot reloading; ensure it is turned off in production.

Summary

Flask local development defaults to http://localhost:5000, and can be started with just a few lines of code, making it suitable for rapid validation of Python APIs.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run