Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Installation

Flask Installation

Before starting to write a Flask application, we need to prepare the Python environment and install Flask.

Installing Flask is fairly straightforward.

Flask is a Python library, so first you need to make sure Python is installed on your computer.


Python version requirements

Flask 3.x requires Python 3.9 or later.

If you haven't installed Python yet, you can download and install it from python.org Download and install.

After installation, open a terminal to verify:



$ python3 --version

Python 3.12.3



$ pip3 --version

pip 24.0 from /usr/lib/python3.12/site-packages/pip (python 3.12)

macOS and most Linux distributions come with Python 3 preinstalled. If you use Windows, be sure to check Add Python to PATH option.


Virtual environment

A virtual environment is a standard practice for Python projects; it creates an isolated Python package space for each project.

Why Use a Virtual Environment

Suppose you have two projects: Project A requires Flask 3.0, and Project B requires Flask 2.3.

If installed globally, the two Flask versions would conflict with each other.

Virtual environments give each project its own independent dependencies, without interfering with each other.

Python's built-in venv module can create virtual environments without installing any extra tools.

Create a virtual environment

First create a project directory, then create a virtual environment inside it:



$ mkdir runoob-flask-test

$ cd runoob-flask-test

$ python3 -m venv .venv

.venv is the directory name of the virtual environment. You can rename it to whatever you like, but .venv is the most common convention in the community.

Activate the virtual environment

Operating system Activation Command
macOS / Linux . .venv/bin/activate or source .venv/bin/activate
Windows (CMD) .venv\Scripts\activate
Windows (PowerShell) .venv\Scripts\Activate.ps1

After successful activation, a (.venv) marker appears before the terminal prompt, indicating the virtual environment is active.



$ . .venv/bin/activate

(.venv) $   # <-- 提示符变了,说明虚拟环境已经激活

Exit the virtual environment

After finishing development, use the deactivate command to exit:



(.venv) $ deactivate

$

Each time you open a new terminal window, you need to reactivate the virtual environment to use Flask.

Alternatively, you can use uv, a Python package and environment management tool. See:https://www.runoob.com/python3/uv-tutorial.html

# 使用指定 Python 版本创建虚拟环境

uv venv --python 3.12 Activate the virtual environment:

macOS / Linux

source .venv/bin/activate

Windows(PowerShell)

.venv\Scripts\activate


Install Flask

With the virtual environment activated, use pip to install Flask:



(.venv) $ pip install Flask

After installation, verify that Flask was installed successfully:

flask --version

Output similar to the following:



Python 3.9.6

Flask 3.0.0

Werkzeug 3.1.5

You can also verify with the following code:



from importlib.metadata import version



flask_version = version("flask")

print(flask_version)

If the installation succeeds, it will output a version number similar to the following:



3.1.3

At this point, the following dependencies are also installed: Werkzeug, Jinja2, Click, itsdangerous, blinker.

Optional: Install python-dotenv

python-dotenv lets you manage environment variables through .env and .flaskenv files, simplifying the use of Flask commands:



(.venv) $ pip install python-dotenv

Although not required, it is strongly recommended for real development. It saves you from typing --app parameters each time, and makes it easier for teams to share environment configurations.


Project directory structure

Before you start coding, it's helpful to understand a standard Flask project directory structure:



myflaskapp/

├── .venv/          # 虚拟环境(不要提交到 Git)

├── app.py           # 应用入口文件

├── templates/       # Jinja2 模板文件目录

├── static/          # CSS、JS、图片等静态文件目录

├── .flaskenv        # Flask 环境变量(可选)

└── .gitignore       # 忽略 .venv 等不需要提交的文件

Virtual environment directory .venv should not be committed to Git. For .gitignore Add to .venv/ .


Development environment

Recommended development environments:

In China, Alibaba and ByteDance also have AI IDEs developed based on VS Code:

ByteDance Trae
A new-generation AI-native development environment launched by ByteDance
Visit ->
Alibaba Qoder
An AI programming IDE launched by Alibaba, deeply customized based on VS Code
Visit ->