glmos-code-explain
glmos-code-explain
Poetry 是一个现代化的 Python 包管理和打包工具,它使用 pyproject.toml
文件来定义项目元数据和依赖关系,并使用 poetry.lock
文件来锁定依赖版本。
Poetry 的主要特点:
依赖管理: 使用 pyproject.toml
声明项目依赖,并支持版本约束。
虚拟环境: 自动创建和管理虚拟环境,隔离项目依赖。
依赖解析: 解决依赖冲突,确保所有依赖都能兼容。
锁定文件: 生成 poetry.lock
文件,精确记录所有依赖的版本和哈希值。
构建和发布: 支持构建和发布 Python 包到 PyPI。
安装
pip install poetry
项目创建
poetry new 项目名
poetry init
国内镜像
[[tool.poetry.source]]
name = "tsinghua"
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
priority = "primary"
关闭打包模式
[tool.poetry]
# 非打包模式,进行依赖管理而不用于打包
package-mode = false
虚拟环境
默认的安装路径在用户目录下的 .cache/pypoetry/virtualenvs
配置下面,则会在项目目录中创建虚拟环境
poetry config virtualenvs.in-project true
安装依赖
poetry add requests
卸载依赖
poetry remove requests
进入虚拟环境
poetry shell
退出虚拟环境
deactivate
不进入虚拟环境,运行代码,可执行
poetry run python xxx.py
poetry.lock
确保项目始终使用相同版本的依赖,避免因依赖版本不一致导致的问题。
poetry.lock 文件如何生成和更新:
poetry install
或 poetry add <package>
时,Poetry 会解析 pyproject.toml
中声明的依赖,并生成 poetry.lock
文件。poetry add <package>
添加新的依赖,或者使用 poetry update
更新现有依赖时,Poetry 会更新 poetry.lock
文件。uv 其实是生成 .venv 这个目录,虚拟环境。
创建项目
uv init xxxx
添加依赖
uv add xxx
同步依赖
uv sync
这里相当于的前端的 npm install
相当于poetry的 poetry install
运行py文件
uv run xxxx.py
使用uv创建Python环境,指定Python版本
//todo 用到了再写