Windows 下超快 Python 包管理器 uv 安装教程

Python 的传统包管理器 pip 安装速度慢、虚拟环境麻烦?别急,今天介绍一个 Rust 编写的现代化包管理器 —— uv,它比 pip 快 10 倍以上,还原生支持虚拟环境、锁文件、缓存优化等功能。

本文将手把手教你如何在 Windows 系统中安装并使用 uv,适合 Python 开发者、数据科学工程师、自动化爱好者。

什么是 uv?

uv 是由 Astral 开发的 Python 包管理工具,目标是替代:

pip(安装依赖)
virtualenv/venv(创建虚拟环境)
pip-tools(生成 requirements.txt)
并做到更快、更简单!

官网地址
https://docs.astral.sh/uv/getting-started/installation/#installation-methods

github地址:
https://github.com/astral-sh/uv/releases

安装uv

方法一:使用 PowerShell 一键安装(推荐 💡)
🛠️ 步骤:

  1. 打开 PowerShell
    可以在“开始菜单”中搜索 PowerShell
    右键 → 以管理员身份运行(推荐)
    执行以下命令安装 uv:
PS C:\Users\Administrator> powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Downloading uv 0.7.13 (x86_64-pc-windows-msvc)
Installing to C:\Users\Administrator\.local\bin
  uv.exe
  uvx.exe
  uvw.exe
everything's installed!

To add C:\Users\Administrator\.local\bin to your PATH, either restart your shell or run:

    set Path=C:\Users\Administrator\.local\bin;%Path%   (cmd)
    $env:Path = "C:\Users\Administrator\.local\bin;$env:Path"   (powershell)

这个脚本执行完后,会下载文件到以下目录
C:\Users\Administrator\.local\bin
image

  1. 并添加到环境变量
PS C:\Users\Administrator>  $env:Path = "C:\Users\Administrator\.local\bin;$env:Path"
PS C:\Users\Administrator> uv --version
uv 0.7.13 (62ed17b23 2025-06-12)
PS C:\Users\Administrator> uv -h
An extremely fast Python package manager.

Usage: uv.exe [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
  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
  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

image


方法二:手动下载安装包(适合不想用脚本的同学)
访问下载地址:uv Releases
https://github.com/astral-sh/uv/releases
找到你的平台对应版本,Windows 64 位一般下载这个:
uv-x86_64-pc-windows-msvc.zip x64 Windows

解压后会得到一个 uv.exe 文件。
将 uv.exe 放入某个路径,并把该路径加入系统环境变量 Path

  1. Mac和Linux系统可以用这条命令:curl -LsSf https://astral.sh/uv/install.sh | sh

卸载uv

缓存文件卸载命令:
Remove-Item -Recurse -Force “$(uv python dir)”
Remove-Item -Recurse -Force “$(uv tool dir)”

uv卸载命令:
Remove-Item “$env:USERPROFILE.local\bin\uv.exe” -Force
Remove-Item “$env:USERPROFILE.local\bin\uvx.exe” -Force

uv删除当前项目下的环境目录(powershell终端)

Remove-Item -Recurse -Force ..venv

bash终端用下面的命令

Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
$ rm -rf ./.venv

参考文档
https://blog.csdn.net/qq_55666050/article/details/148586532

常用命令:

  • 指定python安装目录,自己提前新建出这个目录: $env:UV_PYTHON_INSTALL_DIR = "D:\Program Files\CustomPythonPath"

  • 查看版本列表: uv python list

  • 安装python 3.13: uv python install 3.13 这里会下载到上面指定的目录中
    image
    image

  • 固定到特定的 Python 版本: uv python pin 3.13

  • 卸载python版本:uv python uninstall 3.13

  • 安装老版本的项目依赖包: uv add -r requirements.txt

创建一个项目

  1. 新建一个 D:\code\pyton\uv_demo 目录
  2. 用vs_code打开目录
  3. 在命令行中执行 uv init
Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo
$ uv init
Initialized project `uv-demo`

Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
$ tree
.
|-- README.md
|-- main.py
|-- pyproject.toml 

image

  1. 安装python插件

image

  1. 如果项目根目录下没有.venv目录,执行下面的命令,创建项目虚拟环境
    Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
    $ uv venv
    Using CPython 3.13.5
    Creating virtual environment at: .venv
    Activate with: source .venv/Scripts/activate
    image
  1. 运行 main.py 文件

    Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
    $ uv run main.py
    Using CPython 3.13.5
    Creating virtual environment at: .venv
    Hello from uv-demo!
  2. uv add numpy pandas 安装依赖包
    image

    Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
    $ uv add numpy=3.10.0 pandas
    Resolved 7 packages in 3.17s
    Prepared 6 packages in 5.71s
    ░░░░░░░░░░░░░░░░░░░░ [0/6] Installing wheels...
    warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
          If the cache and target directories are on different filesystems, hardlinking may not be supported.       
          If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
    Installed 6 packages in 1.71s
    + numpy==2.3.0
    + pandas==2.3.0
    + python-dateutil==2.9.0.post0
    + pytz==2025.2
    + six==1.17.0
    + tzdata==2025.2

    安装好后,会记录在 pyproject.toml 文件中
    image

  3. 移除包

    Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
    $ uv remove numpy
    Resolved 7 packages in 28ms
    Audited 6 packages in 0.04ms
  4. 查看库依赖

    Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
    $ uv tree
    Resolved 7 packages in 2ms
    uv-demo v0.1.0
    ├── numpy v2.3.0
    └── pandas v2.3.0
     ├── numpy v2.3.0
     ├── python-dateutil v2.9.0.post0
     │   └── six v1.17.0
     ├── pytz v2025.2
     └── tzdata v2025.2
  5. 自动同步项目环境依赖
    前提是要有 pyproject.tomluv.lock 两个文件

    Administrator@WIN-20240929XKQ MINGW64 /d/code/pyton/uv_demo (master)
    $ uv sync
    Using CPython 3.13.5
    Creating virtual environment at: .venv
    Resolved 7 packages in 2ms
    ░░░░░░░░░░░░░░░░░░░░ [0/6] Installing wheels...
    warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
         If the cache and target directories are on different filesystems, hardlinking may not be supported.
         If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
    Installed 6 packages in 1.78s
    + numpy==2.3.0
    + pandas==2.3.0
    + python-dateutil==2.9.0.post0
    + pytz==2025.2
    + six==1.17.0
    + tzdata==2025.2
  6. 或者从 requirements.txt 中同步项目依赖 ,兼容老项目
    uv add -r requirements.txt
    image

  1. 虚拟环境管理
    参考文档:
    https://www.cnblogs.com/Flat-White/p/18920080

  2. 如何切换项目中使用的 python 版本

13.1 修改版本
如项目下 .python-version文件中记录的当前项目的版本为3.10`
image

13.2 删除 .venv 虚拟环境目录
rm -rf .venv

13.3 切换 python 版本为 3.13
uv python pin 3.13

13.4 修改 pyproject.tomluv.lock 两个文件里的python版本为 3.13
requires-python = ">=3.13"

13.5 同步依赖
uv sync

至此已经切换项目版本成功

下以是执行过程

Administrator@WIN-20240929XKQ MINGW64 D:/Program Files (x86)/Microsoft VS Code (master)
$ rm -rf .venv
(uv-demo)
Administrator@WIN-20240929XKQ MINGW64 D:/Program Files (x86)/Microsoft VS Code (master)
$ uv python pin 3.13
Updated `.python-version` from `3.10` -> `3.13`
(uv-demo) 
Administrator@WIN-20240929XKQ MINGW64 D:/Program Files (x86)/Microsoft VS Code (master)
$ uv sync
Using CPython 3.13.5
Creating virtual environment at: .venv
Resolved 12 packages in 58ms
Prepared 3 packages in 3.80s
░░░░░░░░░░░░░░░░░░░░ [0/11] Installing wheels...
warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
         If the cache and target directories are on different filesystems, hardlinking may not be supported.
         If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
Installed 11 packages in 1.54s
 + certifi==2025.4.26
 + charset-normalizer==3.4.2
 + idna==3.10
 + numpy==2.3.0
 + pandas==2.3.0
 + python-dateutil==2.9.0.post0
 + pytz==2025.2
 + requests==2.32.3
 + six==1.17.0
 + tzdata==2025.2
 + urllib3==2.4.0
(uv-demo) 

参考视频
https://www.bilibili.com/video/BV15MVdzaEUw/?spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=a68414cd60fe26e829ce1cdd4d75a9e6