文章目录
uv基于python和pip,比pip更快。
安装
bash
最全的命令:
python -m pip install uv -i https://pypi.tuna.tsinghua.edu.cn/simple
如果已经配置好pip环境变量、全局镜像等,直接使用如下命令:
pip install uv
然后cmd中输入uv或uv --version,则表示成功。
是否需要单独添加环境变量呢?
不需要,因为python下的Scripts如果已经添加到环境变量,uv.exe也在这个目录下。
是否需要配置全局环境变量呢?
如果pip配置了环境变量,uv不需要单独配置,和pip用的是同一环境变量。
创建项目
主要有两种情况:
1、uv创建项目,然后用pycharm打开。
2、已有pycharm项目,uv进行接管。
创建项目-uv创建项目,然后用pycharm打开
1、初始化一个项目。
python
uv init uv-demo
就会创建一个uv-demo文件夹,里面的结构为:
python
.python-version # 这是个隐藏文件
main.py
pyproject.toml
README.md
2、pycharm打开该文件夹
3、添加一个依赖(注意:是添加不是安装,add和install是不一样的)
python
uv add requests
已有pycharm项目,uv进行接管
1、关闭pycharm(避免缓存等干扰)
2、删除旧的.venv文件夹
3、运行无覆盖的初始化命令
--no-readme 表示不覆盖readme文件
--no-package 告诉 uv 这是一个应用程序项目(而不是一个要发布的库),避免创建 src/ 目录结构,保持你现有的扁平结构不变。
python
uv init --no-readme --no-license --no-package
4、requirement.txt转换为uv.lock
bash
# 将 requirements.txt 转换为 uv.lock 并安装
uv pip compile requirements.txt -o uv.lock
uv sync
这样会保留requirements.txt。
如果不想保留,也可以直接uv sync。
5、同步环境 (关键步骤)
运行 uv sync。这是 uv 的核心命令,它会:
(1)读取 pyproject.toml (和 uv.lock)。
(2)自动创建 一个新的 .venv 文件夹(由 uv 管理,速度极快)。
(3)安装所有定义的依赖。
bash
uv sync
此时,你会发现目录下多了一个 .venv 和一个 uv.lock 文件。
其他
文档
astral官网地址:
https://astral.sh/
astral官网-uv文档地址(速度挺快的):
https://docs.astral.sh/uv/
uv帮助文档
cmd输入uv,返回结果如下:
bash
An extremely fast Python package manager.
Usage: uv [OPTIONS] <COMMAND>
Commands:
auth Manage authentication
run Run a command or script
init Create a new project
add Add dependencies to the project
remove Remove dependencies from the project
version Read or update the project's version
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
format Format Python code in the project
audit Audit the project's dependencies
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
build Build Python packages into source distributions and wheels
publish Upload distributions to an index
cache Manage uv's cache
self Manage the uv executable
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:
--managed-python Require use of uv-managed Python versions [env: UV_MANAGED_PYTHON=]
--no-managed-python Disable use of uv-managed Python versions [env: UV_NO_MANAGED_PYTHON=]
--no-python-downloads Disable automatic downloads of Python. [env: "UV_PYTHON_DOWNLOADS=never"]
Global options:
-q, --quiet...
Use quiet output
-v, --verbose...
Use verbose output
--color <COLOR_CHOICE>
Control the use of color in output [possible values: auto, always, never]
--native-tls
Whether to load TLS certificates from the platform's native store [env: UV_NATIVE_TLS=]
--offline
Disable network access [env: UV_OFFLINE=]
--allow-insecure-host <ALLOW_INSECURE_HOST>
Allow insecure connections to a host [env: UV_INSECURE_HOST=]
--no-progress
Hide all progress outputs [env: UV_NO_PROGRESS=]
--directory <DIRECTORY>
Change to the given directory prior to running the command [env: UV_WORKING_DIR=]
--project <PROJECT>
Discover a project in the given directory [env: UV_PROJECT=]
--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.