uv使用笔记(python包的管理工具)

文章目录

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.
相关推荐
花 满 楼10 小时前
EB引脚配置自动化方案|告别手动繁琐配PIN,Excel+Python批量生成ARXML实现一键配置
python·excel·eb
江华森10 小时前
jieba 分词 + 共现矩阵构建——《釜山行》人物关系提取(二)
python
用户2986985301410 小时前
Python 实现 Excel 新旧格式互转:告别 Office 依赖的自动化方案
后端·python·excel
进击切图仔10 小时前
基于千问的白盒蒸馏 SOP
运维·服务器·人工智能·python·效率
2401_8685347810 小时前
案例介绍-小型网络规划及搭
python·计算机网络
ONE_SIX_MIX10 小时前
lightweight-charts-onesixth v2.8.1 → v3.1.1 更新总览
java·前端·python
YUS云生10 小时前
Python学习笔记·第29天:Git进阶——分支操作与合并冲突
笔记·python·学习
xiaoyuchidayuma10 小时前
已知 ud、uq 求解三相相电压并计算调制比
笔记·学习
奋斗的小方10 小时前
Java进阶篇1-3:集合框架(单列集合)
java·windows·python
B8017913Y11 小时前
上课记笔记写不完不会整理?2026学习笔记生成使用场景该怎么选
人工智能·笔记·学习