新python环境和项目管理工具-rust uv
安装
按照官方指引,可以全局安装,也可以在具体的pyhon解释器用pip安装(换句话说可以和全局解释器共存,比如类似miniconda的版本管理器)
https://github.com/astral-sh/uv
Rust出品,属实精品!
使用
初看
(base) [root@VM-113-76-centos /data/projects/personal]# uv -h
An extremely fast Python package manager.
Usage: uv [OPTIONS] <COMMAND>
Commands:
run Run a command or script
init Create a new project
add Add dependencies to the project
remove Remove dependencies from the project
sync Update the project's environment
lock Update the project's lockfile
export Export the project's lockfile to an alternate format
tree Display the project's dependency tree
tool Run and install commands provided by Python packages
python Manage Python versions and installations
pip Manage Python packages with a pip-compatible interface
venv Create a virtual environment
cache Manage uv's cache
self Manage the uv executable
version Display uv's version
help Display documentation for a command
Cache options:
-n, --no-cache Avoid reading from or writing to the cache, instead using a temporary directory for the duration of the operation [env: UV_NO_CACHE=]
--cache-dir <CACHE_DIR> Path to the cache directory [env: UV_CACHE_DIR=]
Python options:
--python-preference <PYTHON_PREFERENCE> Whether to prefer uv-managed or system Python installations [env: UV_PYTHON_PREFERENCE=] [possible values: only-managed, managed, system, only-system]
--no-python-downloads Disable automatic downloads of Python. [env: "UV_PYTHON_DOWNLOADS=never"]
Global options:
-q, --quiet Do not print any output
-v, --verbose... Use verbose output
--color <COLOR_CHOICE> Control colors in output [default: auto] [possible values: auto, always, never]
--native-tls Whether to load TLS certificates from the platform's native certificate store [env: UV_NATIVE_TLS=]
--offline Disable network access
--no-progress Hide all progress outputs
--config-file <CONFIG_FILE> The path to a `uv.toml` file to use for configuration [env: UV_CONFIG_FILE=]
--no-config Avoid discovering configuration files (`pyproject.toml`, `uv.toml`) [env: UV_NO_CONFIG=]
-h, --help Display the concise help for this command
-V, --version Display the uv version
Use `uv help` for more details.
管理
开发项目的话,使用uv init
uv init 你的项目名
查看
(base) [root@VM-113-76-centos /data/projects/personal/py-demon]# tree
.
├── hello.py
├── pyproject.toml
├── README.md
└── uv.lock
0 directories, 4 files
- uv.lock是锁定的依赖,类似于pip freeze
- pyproject.toml是项目的配置文件, 里面有依赖的版本信息和python版本信息
加包
uv add requests
通过uv安装包, 会进一步更新lock文件, 也会更新pyproject.toml文件, 当迁移项目时,只需要将这两个文件拷贝到新环境即可
运行或同步
uv run hello.py
或者
uv sync
补充
这里要特别说明, 使用uv管理的项目,都要使用uv run xxx.py
来运行,而不是直接使用python xxx.py, 因为uv会自动激活虚拟环境,而python不会
2024-09-23补充
对于已有项目,使用如下步骤进行环境统一
1. 初使化环境
uv init --python=3.12
2. 如果当前shell的解释器不是3.12,需要切换到3.12或者使用venv
uv venv python=3.12
3. 添加依赖
uv add requests
4. 使用
uv run hello.py
总结
uv做为python项目管理,兼顾效率,稳定性,相对以前的venv(需要手动激活shell path)和miconda的不足(容易互相污染各解释器的安装包环境),加上rust的加持,是非常方便的整体方案解决利器,推荐使用
- 原文作者:大鱼
- 原文链接:https://brucedone.com/archives/1703/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。