python-env

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Environment

Python环境管理

Fast Python environment management with uv.
使用uv进行快速Python环境管理。

Quick Commands

快速命令

TaskCommand
Create venv
uv venv
Install package
uv pip install requests
Install from requirements
uv pip install -r requirements.txt
Run script
uv run python script.py
Show installed
uv pip list
任务命令
创建虚拟环境
uv venv
安装包
uv pip install requests
从requirements文件安装
uv pip install -r requirements.txt
运行脚本
uv run python script.py
查看已安装包
uv pip list

Virtual Environment

虚拟环境

bash
undefined
bash
undefined

Create venv (instant)

创建虚拟环境(瞬间完成)

uv venv
uv venv

Create with specific Python

使用指定Python版本创建

uv venv --python 3.11
uv venv --python 3.11

Activate (or use uv run)

激活虚拟环境(或使用uv run)

source .venv/bin/activate # Unix .venv\Scripts\activate # Windows
undefined
source .venv/bin/activate # Unix系统 .venv\Scripts\activate # Windows系统
undefined

Package Installation

包安装

bash
undefined
bash
undefined

Single package

安装单个包

uv pip install requests
uv pip install requests

Multiple packages

安装多个包

uv pip install flask sqlalchemy pytest
uv pip install flask sqlalchemy pytest

With extras

安装包含额外依赖的包

uv pip install "fastapi[all]"
uv pip install "fastapi[all]"

Version constraints

带版本约束安装

uv pip install "django>=4.0,<5.0"
uv pip install "django>=4.0,<5.0"

Uninstall

卸载包

uv pip uninstall requests
undefined
uv pip uninstall requests
undefined

Minimal pyproject.toml

最简pyproject.toml配置

toml
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
    "httpx>=0.25",
    "pydantic>=2.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "ruff>=0.1",
]
toml
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
    "httpx>=0.25",
    "pydantic>=2.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "ruff>=0.1",
]

Project Setup Checklist

项目设置检查清单

bash
mkdir my-project && cd my-project
uv venv
bash
mkdir my-project && cd my-project
uv venv

Create pyproject.toml

创建pyproject.toml文件

uv pip install -e ".[dev]" uv pip list
undefined
uv pip install -e ".[dev]" uv pip list
undefined

Troubleshooting

故障排除

IssueSolution
"No Python found"
uv python install 3.11
Wrong Python version
uv venv --python 3.11
Conflicting deps
uv pip compile --resolver=backtracking
Cache issues
uv cache clean
问题解决方案
"未找到Python"
uv python install 3.11
Python版本错误
uv venv --python 3.11
依赖冲突
uv pip compile --resolver=backtracking
缓存问题
uv cache clean

When to Use

使用场景

  • Always use uv over pip for speed
  • Creating virtual environments
  • Installing packages
  • Managing dependencies
  • Running scripts in project context
  • 始终优先使用uv而非pip以提升速度
  • 创建虚拟环境
  • 安装包
  • 管理依赖项
  • 在项目上下文中运行脚本

Additional Resources

额外资源

For detailed patterns, load:
  • ./references/pyproject-patterns.md
    - Full pyproject.toml examples, tool configs
  • ./references/dependency-management.md
    - Lock files, workspaces, private packages
  • ./references/publishing.md
    - PyPI publishing, versioning, CI/CD

如需详细模式,请查看:
  • ./references/pyproject-patterns.md
    - 完整pyproject.toml示例、工具配置
  • ./references/dependency-management.md
    - 锁定文件、工作区、私有包
  • ./references/publishing.md
    - PyPI发布、版本控制、CI/CD

See Also

相关技能

This is a foundation skill with no prerequisites.
Build on this skill:
  • python-typing-patterns
    - Type hints for projects
  • python-pytest-patterns
    - Testing infrastructure
  • python-fastapi-patterns
    - Web API development
这是一项基础技能,无前置要求。
可基于此技能拓展:
  • python-typing-patterns
    - 项目类型提示
  • python-pytest-patterns
    - 测试基础设施
  • python-fastapi-patterns
    - Web API开发