Python Overview & Environment Setup
Why Python is everywhere in backend development, CPython vs alternatives, installing Python, virtual environments with venv, dependency management with pip, and choosing an IDE.
Why Python for the Backend
Python is a dynamically typed, interpreted language known for readable syntax and an enormous ecosystem. On the backend it powers web frameworks (Django, FastAPI, Flask), data tooling, scripting, automation, AI services and more — the same language handles business logic and data pipelines.
- Django: batteries-included full-stack framework, ideal for content sites and admin systems.
- FastAPI: async, high-performance API framework with automatic OpenAPI docs.
- Flask: minimal micro-framework when you want full control.
Installing Python
Download CPython 3.12+ from python.org, or use a package manager. On Windows tick 'Add Python to PATH'; on Linux check python3 --version.
python3 --version # 查看版本
python3 -m pip --version
# Windows (winget)
winget install Python.Python.3.12
# Linux (apt)
sudo apt install python3 python3-pip python3-venv
Virtual Environments and pip
Never install project dependencies into the system Python — create an isolated virtual environment per project. Lock versions in requirements.txt (or pyproject.toml with uv/poetry) so production matches development.
python3 -m venv .venv # 创建虚拟环境
# 激活
source .venv/bin/activate # Linux / macOS
.venv\Scripts\activate # Windows
pip install fastapi uvicorn # 安装依赖
pip freeze > requirements.txt # 锁定版本
pip install -r requirements.txt # 还原依赖
# 国内可加镜像加速
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple fastapi
First Program and Code Style
def greet(name: str) -> str:
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("BackendWiki"))
- PEP 8 is the style guide: 4-space indent, snake_case functions, UPPER_CASE constants.
- Type hints (def greet(name: str) -> str) are optional but standard in modern code; mypy can check them.
后端为什么用 Python
Python 是动态类型的解释型语言,以语法可读性和庞大生态著称。后端领域它支撑 Django、FastAPI、Flask 等 Web 框架,以及数据处理、脚本自动化、AI 服务——同一门语言就能同时写业务逻辑和数据管线。
- Django:全家桶式全栈框架,适合内容站与管理后台。
- FastAPI:异步高性能 API 框架,自动生成 OpenAPI 文档。
- Flask:极简微框架,适合需要完全掌控的项目。
安装 Python
从 python.org 下载 CPython 3.12+,或用系统包管理器安装。Windows 安装时勾选「Add Python to PATH」;Linux 用 python3 --version 检查。
python3 --version # 查看版本
python3 -m pip --version
# Windows (winget)
winget install Python.Python.3.12
# Linux (apt)
sudo apt install python3 python3-pip python3-venv
虚拟环境与 pip
不要把项目依赖装进系统 Python——每个项目用独立虚拟环境。把版本锁定写进 requirements.txt(或用 uv/poetry 管理 pyproject.toml),保证生产与开发一致。
python3 -m venv .venv # 创建虚拟环境
# 激活
source .venv/bin/activate # Linux / macOS
.venv\Scripts\activate # Windows
pip install fastapi uvicorn # 安装依赖
pip freeze > requirements.txt # 锁定版本
pip install -r requirements.txt # 还原依赖
# 国内可加镜像加速
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple fastapi
第一个程序与代码风格
def greet(name: str) -> str:
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("BackendWiki"))
- PEP 8 是官方代码风格:4 空格缩进、函数 snake_case、常量全大写。
- 类型注解(def greet(name: str) -> str)可选但已是现代写法标配,可用 mypy 检查。