dev: Move from poetry to uv

Thanks to Rakuto, I’ve made the switch from Poetry which was nice and worked well to uv which is from the folks at Astral, and even faster. The move to ruff and uv has been great although a bit of work. Here are some notes:

  1. uv is written in Rust and is much faster than poetry, like keystroke fast. The main difference is that what it needs a pyproject.toml is quite different and you have to essentially rewrite it.
  2. The base commands are pretty similar to poetry, you do a uv add to add a package to the project section of pyproject.toml
  3. Overall it’s not that big a change but it took some time to figure out. I’m sure there are tools to convert things, but we have a Makefile with our default packages and a nice ENV variable thing where you just fill out how the different venv systems add, install and run things in Rich’s Famous lib.

Direnv virtual environments

The trickiest part is that poetry and pipenv provide a nice way to have a per-directory Python environment. When there is a change in a directory it picks it up and you are in that environment.

This package uv does not have support for that. There are many ways to do this, but I found that using https://direnv.net/ is the best way to get this done. What you do is edit the .envrc file in the top parent directory

There are two tricks that we have for this file that are documented here. The first is the use of asdf to manage executable versions. It uses the versions in .tool-versions so each directory can pin a version.

The second mouthful, I’m quite proud of and I can’t remember how I found it, but keep this little bash gem forever, it sticks the current working directory into a bash variable. And the first % deletes everything after the word git and the second the word git itself to give you a workspace. This is a concept taken from @sam where you can have multiple workspaces (eg different companies or projects) and you can switch between them. It has the same structure so the typical is ~/ws/git/src which is the mono repo with all your code and data it lives in ~/ws/data and so forth.

The second trick is that uv create a standard .venv and you have to source the activate script, so look for it and then run it.

The nice thing about direnv is that when you exit a directory it unloads (really I think it exits a subshell), so you are automatically out of the environment. This actually much faster than pipenv which does the same thing but very slowly. And poetry shell does similar things as well, but automatically, but this venv trick with direnv is great.

Note that if you use powerline or other status line utilities, you can see if you are in a subshell.

# asdf manages versions of various executables
use asdf

# this is quite a mouthful, but basically it
# quickly looks up through all the parents
# of the current directory and finds git
# it assume the directory above that is the
# current workspace called "ws" by @sam
wd="$(PWD)" && export WS_DIR="${pwd%git${pwd#*git}}"

# some nice magic to look for the .venv and activate
if [[ -r .venv/bin/activate ]]; then source .venv/bin/activate; fi

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect

Recent posts

Loading Mastodon feed…