Flask Installation

Before we start writing Flask applications, we need to prepare the Python environment and install Flask.

Installing Flask is fairly straightforward.

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


Python version requirements

Flask 3.x RequirementsPython 3.9 and above。

If you haven't installed Python yet, you can download it frompython.orgDownload 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 already come with Python 3 preinstalled. If you are using Windows, please check the option during installation.Add Python to PATHOptions.


Virtual environment

A virtual environment is a standard practice for Python projects. It creates an independent Python package space for each project.

Why use a virtual environment

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

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

Virtual environments allow each project to have its own independent dependencies without interfering with one another.

Python's built-invenvThe module can create virtual environments without installing additional tools.

Create a virtual environment

First create the project directory, then create a virtual environment within it:

$ mkdir example-flask-test
$ cd example-flask-test
$ python3 -m venv .venv

.venvis the directory name of the virtual environment. You can change it to a name you like, but.venvis the most common convention in the community.

Activate the virtual environment

Operating system Activation command
macOS / Linux . .venv/bin/activateorsource .venv/bin/activate
Windows (CMD) .venv\Scripts\activate
Windows (PowerShell) .venv\Scripts\Activate.ps1

After successful activation, the terminal prompt will be prefixed with(.venv)indicator, indicating that the virtual environment is active.

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

Exit the virtual environment

After development is complete, usedeactivateCommand to exit:

(.venv) $ deactivate
$

Every time you open a new terminal window, you need to reactivate the virtual environment before using Flask.

You can also use uv, a Python package and environment management tool. See:https://www.example.com/python3/uv-tutorial.html

# 使用指定 Python 版本创建虚拟环境
uv venv --python 3.12
激活虚拟环境:

# 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 whether Flask was installed successfully:

flask --version

The output will be 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 installed successfully, it will output a version number similar to the following:

3.1.3

At this point, the following dependency packages are also installed along with it:Werkzeug、Jinja2、Click、itsdangerous、blinker。

Optional: Install python-dotenv

python-dotenvAllows you to.envand.flaskenvManage environment variables with a file, simplifying the use of Flask commands:

(.venv) $ pip install python-dotenv

Although not required, it is strongly recommended in actual development. It saves you from entering--appparameters every time, and also makes it easier for teams to share environment configuration.


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.venvshould not be committed to Git. In.gitignoreAdd to.venv/That's it.


Development environment

Recommended development environment:

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

Online AI application generation and development platform:

other extensions