Rye: An Alternate and Possibly Better Option for Python Package Management

z68z68
3 min read

Rye is a Python package management tool released by Armin Ronacher (creator of Flask) in May 2023. There are already many Python package management tools on the market, such as Pipenv and Poetry. Armin Ronacher himself hasn't made any comparisons between Rye and existing tools, but he still wrote an issue (https://github.com/mitsuhiko/rye/discussions/6) sharing why he wanted to develop Rye.

I have tried Rye for a short time and applied it to several projects, finding a few features that I am very satisfied with:

  • Rye can help you install Python

  • Rye's resolver performance seems better than Pipenv

However, I have also encountered some bugs with Rye, such as:

  • https://github.com/mitsuhiko/rye/issues/91

But since Rye is still an early and active project, this bug seems to have been fixed in a recent release.

Install

Rye is a software developed based on Rust, so it requires installation using cargo, which is a good thing for me, as cargo is a million times better than brew.

In the latest release, users don't even need to touch cargo, just need to:

# install rye in 1 line
$ curl -sSf https://rye-up.com/get | bash

Next, in the project where you want to use Rye, you can use the following command to specify the Python version for this project:

# init rye in your new python project
$ rye init

# pin the python version
$ rye pin 3.10 # or 3.11 / 3.9 / 3.8

Next, every time you want to install a new package, use the following command (assuming we want to install flask):

# add python packages
$ rye add "flask>=2.0"

# let rye do the rest of works
$ rye sync

rye add adds the dependency to pyproject.toml, and rye sync downloads and installs the required python/package, then generates the corresponding lock file.

Finally, in the project, we can also use a command similar to pipenv shell to open a virtual environment.

$ rye shell

Install Python

In fact, I am not yet sure whether rye can unify Python package management. The main reason is that existing solutions (pipenv, poetry) have been improving over time, and these solutions have undergone many production-ready tests.

What I like most about rye is that it can help us install Python. In the era without rye, the methods of installing Python were quite bizarre. We might need to download binaries from the Python official website and link them ourselves, download tar files and build them ourselves, or even use brew to install on a Mac. No matter which method, the developer experience is terrible, and we often mix up the Python we need for development with the system's built-in Python.

The commands to install and remove Python with rye are as follows:

# install python3.8.5 on your system
rye toolchain fetch cpython@3.8.5

# uninstall python3.8.5 on your system
rye toolchain remove cpython@3.8.5

We can also use the list command to see what rye has installed for us.

rye toolchain list
rye toolchain list --include-downloadable

No more brew and no more system Python!

Ref

5
Subscribe to my newsletter

Read articles from z68 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

z68
z68

Machine Learning Engineer, interest in MLOps, Recommendation System, Deep Learning, Large Language Model, Natural Language Processing, Distributed System, Kubernetes, Databases, Data Engineering and all the Engineering Best Practices.