Python 服务器

发布于 更新于

AI总结: 本文介绍了如何使用 uv 工具快速生成和管理 Python 项目,包括项目结构初始化、虚拟环境创建、依赖管理和版本切换等功能。用户可以通过 uv init 命令创建新项目,使用 uv venv 创建虚拟环境,并通过 uv add 添加依赖。对于已有项目,用户可以同步环境、更新依赖、查看依赖树,并检测无用依赖。 优化建议: 1. 在创建虚拟环境时,建议明确选择 Python 版本,避免使用默认设置,确保与项目需求一致。 2. 定期使用 uv sync 命令同步依赖,确保环境与配置文件一致,减少潜在问题。 3. 在添加依赖时,考虑使用 requirements.txt 文件,确保团队成员能够一致地安装相同的库版本。 4. 定期检测并清理无用依赖,保持项目整洁,避免不必要的包影响性能。 5. 对于依赖升级,建议在升级前查看兼容性,确保新版本不会引入破坏性更改。
uv pip freeze > requirements.txt
uv pip list --outdated
uv pip install --upgrade urllib3
uv tree
# 指定安装镜像 默认到github下载, 使用时查询当前可用的加速镜像地址
export UV_PYTHON_INSTALL_MIRROR=https://github.com/indygreg/python-build-standalone/releases/download
uv python install 3.11    # 安装指定版本
uv python use 3.11        # 切换当前环境版本

uv pip install deptry

# deptry .
Scanning 7 files...

pyproject.toml: DEP002 'flask' defined as a dependency but not used in the codebase
pyproject.toml: DEP002 'loguru' defined as a dependency but not used in the codebase
Found 2 dependency issues.