Following my previous post, today I wanted to see if I could replace my pyenv
and pyenv-virtualenv
usage with uv
.
Install uv
If you haven’t done it yet, you need to first install uv
using either this method or one of the methods described in the documentation:
|
|
Install Python
uv
can detect Python versions installed in different ways in the system or it can install its own copies. You can check which ones are installed, using this command:
|
|
Now we are going to install the latest (at the time of writing) version of Python:
|
|
Create a virtual environment
Creating virtual environment with uv
is quite easy. We can do it in this way:
|
|
With --python 3.13.1
we specify the Python version we want and with --prompt my-project
we customise the text that will appear in the prompt.
As the output will say, we can simply activate the environment with this command:
|
|
Automatically activate the virtual environment
Running source .venv/bin/activate
every time we enter the project directory can be boring. We can automate this by using direnv.
direnv
is a tool which can automatically set environment variables or run simple commands when we enter inside a directory.
You can install it with:
|
|
Once it’s installed, we need to add this configuration inside our .zshrc
:
|
|
or, in case we are using bash
, we need to do it inside .bashrc
:
|
|
At this point simply close and reopen your terminal (it’s the quickest way to reload the configuration) and finally create a file named .envrc
inside your project folder, containing these two lines (or append these two lines in case you are already using this file):
|
|
After you save this file and you get back to your terminal, you will get an error like this:
|
|
Just run that command to allow direnv to load your configuration:
|
|
Installing Python packages
At this point you can use uv pip
(which is a drop in replacement for pip
with a few limitations) just like you would use pip
:
|
|
Make an alias for uv pip
If you are happy to use the uv
version of pip
, you can use it anywhere, even in virtual environments which are not managed by uv
. You can create an alias by adding this to your .zshrc
(or .bashrc
etc…)
|
|
This way, every time you invoke pip
, you will use the uv
version:
|
|
Conclusion
Using uv pip
, uv venv
and uv python
you can definitely speed up Python installation, virtual environments creations and Python packages installation.
But uv
is much more. You can also manage dependencies, lock them and do many other things. At this point I don’t feel like switching completely to this tool, especially because a few things (like dependencies locking) are not standard across Python ecosystem and the risk is to use a workflow which is not supported by other existing tools (for example if you are using dependabot you won’t be able to just use uv.lock
file, you will need to extract requirements.txt
files)
My final advice is to use just the parts which are compatible with your existing workflow and wait until a standard for locking dependencies will be universally accepted, before jumping in with both feet.