Python3 pip

Installing, freezing, pinning, offline installs and the dependency-hell escape hatches.

Beginner
Key PointsPin exact versions in production, use `python -m pip` to be sure which interpreter you are installing into.

Basic Commands

Bash
python -m pip install requests
python -m pip install "fastapi>=0.110,<1.0"
python -m pip install --upgrade pip
python -m pip uninstall requests
python -m pip list
python -m pip show requests
pip alone may point at a different interpreter than python. Always write python -m pip inside a venv.

Reproducible Installs

Bash
# 导出当前环境的精确版本
python -m pip freeze > requirements.txt

# 还原
python -m pip install -r requirements.txt

# 只装依赖树里的顶层包(不含传递依赖)
pip install pip-tools
pip-compile requirements.in -o requirements.txt

Mirrors and Offline

Bash
# 国内镜像
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

# 永久配置
python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 离线:先下载 wheel,再到目标机器安装
python -m pip download -d ./wheels -r requirements.txt
python -m pip install --no-index --find-links=./wheels -r requirements.txt

When Installs Fail

SymptomCauseFix
Building wheel failedNo prebuilt wheel for your platformInstall build tools, or pin an older version
Permission deniedInstalling into system PythonUse a venv
VersionConflictTwo packages need different versionspip-compile / uv lock
SSL errorCorporate proxy or old certifiSet --proxy / upgrade certifi
pip install never downgrades an existing package unless you ask. If an import suddenly breaks, check pip list for a half-upgraded tree.

Friends & Partners