从Pyenv切换到Uv
Switching from Pyenv to Uv

原始链接: https://bluesock.org/~willkg/blog/dev/switch_pyenv_to_uv.html

作者在 Ubuntu 24.04 上从 pyenv、pip、pipx 及相关工具迁移到 uv,并记录了整个过程。最初,系统使用 pyenv 管理多个 Python 版本,并通过 pipx 和默认 pip 环境安装大量全局包。首先,作者记录了现有环境的配置。然后,按照官方说明卸载了 pipx 包和 pyenv 本身。使用独立安装程序安装 uv,并配置了 shell 自动补全。之前由 pyenv 管理的 Python 版本使用 `uv python install` 重新安装。创建了一个自定义脚本 `uv-python-symlink` 来维护 "pythonX.Y" 符号链接,以便轻松访问特定 Python 版本。之前由 pipx 管理的工具使用 `uv tool install` 安装。tox 需要特殊处理,需在 tox 环境中安装 `tox-uv`。最终结果是一个简化的设置,由 uv 管理 Python 版本、全局工具和虚拟环境,提供更简化且可能更快的流程。

这篇 Hacker News 帖子讨论了新型 Python 包管理器 “uv” 的日益普及,它旨在取代 pyenv、pip、conda 和 poetry 等工具。用户称赞 uv 在依赖项解析和安装方面的速度,使 Python 开发更快更令人愉快。 重点突出的功能包括其直观的命令行界面 (CLI)、支持工作区的单体仓库 (monorepo)、通过 “uv tool install” 替代 pipx,以及新颖的 “uv run” 命令,该命令可以直接从脚本注释 (PEP 723) 自动创建临时虚拟环境并安装必要的依赖项。 虽然一些用户提到了与 GPU 特定依赖项相关的次要问题,以及考虑到其背后的公司,可能存在的盈利问题,但总体情绪 overwhelmingly positive。许多人认为 uv 极大地改善了 Python 开发人员的体验,甚至呼吁现有工具承认 uv 是一个更优的解决方案。

原文

Premise

The 0.4.0 release of uv does everything I currently do with pip, pyenv, pipx, pip-tools, and pipdeptree. Because of that, I'm in the process of switching to uv.

This blog post covers switching from pyenv to uv.

History

  • 2024-08-29: Initial writing.

  • 2024-09-12: Minor updates and publishing.

  • 2024-09-20: Rename uv-sync (which is confusing) to uv-python-symlink.

Start state

I'm running Ubuntu Linux 24.04. I have pyenv installed using the the automatic installer. pyenv is located in $HOME/.pyenv/bin/.

I have the following Pythons installed with pyenv:

I'm not sure why I have 3.7 still installed. I don't think I use that for anything.

My default version is 3.10.14 for some reason. I'm not sure why I haven't updated that to 3.12, yet.

In my 3.10.14, I have the following Python packages installed:

That probably means I installed the following in the Python 3.10.14 Python environment:

  • MozPhab

  • pipx

  • virtualenvwrapper

Maybe I installed some other things for some reason lost in the sands of time.

Then I had a whole bunch of things installed with pipx.

I have many open source projects all of which have a .python-version file listing the Python versions the project uses.

I think that covers the start state.

Steps

First, I made a list of things I had.

  • I listed all the versions of Python I have installed so I know what I need to reinstall with uv.

  • I listed all the packages I have installed in my 3.10.14 environment (the default one).

  • I listed all the packages I installed with pipx.

I uninstalled all the packages I installed with pipx.

Then I uninstalled pyenv and everything it uses. I followed the pyenv uninstall instructions:

Then I removed the bits in my shell that add to the PATH and set up pyenv and virtualenvwrapper.

Then I started a new shell that didn't have all the pyenv and virtualenvwrapper stuff in it.

Then I installed uv using the uv standalone installer.

Then I ran uv --version to make sure it was installed.

Then I installed the shell autocompletion.

Then I started a new shell to pick up those changes.

Then I installed Python versions:

When I type "python", I want it to be a Python managed by uv. Also, I like having "pythonX.Y" symlinks, so I created a uv-python-symlink-sync script which creates symlinks to uv-managed Python versions:

https://github.com/willkg/dotfiles/blob/main/dotfiles/bin/uv-python-symlink

Then I installed all my tools using uv tool install.

For tox, I had to install the tox-uv package in the tox environment:

Now I've got everything I do mostly working.

So what does that give me?

I installed uv and I can upgrade uv using uv self update.

Python interpreters are managed using uv python. I can create symlinks to interpreters using uv-sync script. Adding new interpreters and removing old ones is pretty straight-forward.

When I type python, it opens up a Python shell with the latest uv-managed Python version. I can type pythonX.Y and get specific shells.

I can use tools written in Python and manage them with uv tool including ones where I want to install them in an "editable" mode.

I can write scripts that require dependencies and it's a lot easier to run them now.

I can create and manage virtual environments with uv venv.

Next steps

Delete all the .python-version files I've got.

Update documentation for my projects and add a uv tool install PACKAGE option to installation instructions.

Probably discover some additional things to add to this doc.

Thanks

Thank you to the Astral crew who wrote uv.

Thank you to Rob Hudson who goaded me into posting this finally rather than sit on it another month.

联系我们 contact @ memedata.com