SoFunction
Updated on 2025-03-02

How does Python project management tool Rye work

yesPEP 518A file format proposed. It contains the requirements of the Python project construction system and some configurations of the project. This has a document showing the structure of this file.Declaring project metadata — Python Packaging User GuideThis file can be composed of multiple paragraphs. inprojectThe paragraphdependenciesIndicates the dependency of the project.

rye philosophy

Recommend to read this articlePhilosophy - Rye ()This article demonstrates Rye’s design philosophy. This contains some of the advantages of rye. For example

  • You can manage dependencies without installing pip on the virtual environment.
  • Not using system python.

Rye installation and configuration shell and basic usage methods

How to install and configure shells

Installation - Rye ()The core is the following two lines.

curl -sSf /get | bash
echo 'source "$HOME/.rye/env"' >> ~/.bashrc

The first line is installation.

  • from/getDownload the script and run it.
  • The core step in the script is to download a binary file, run the binary file, and install it. As for how this binary file is installed into the system, we will explain in detail later in the article.

The second line is to configure the shell (the content is the configuration environment variable). The core content is$HOME/.rye/shimsAdd to the front of $PATH.

➜  ~ cat "$HOME/.rye/env"
# rye shell setup
case ":${PATH}:" in
  *:"$HOME/.rye/shims":*)
    ;;
  *)
    export PATH="$HOME/.rye/shims:$PATH"
    ;;
esac

What is actually installed by Rye

The default path to install rye is~/.rye. The general directory structure of rye is as follows (the following is what I have used after installing it on my computer. There may be a few things missing when I first installed it.):

➜  ~ tree -L 2 ~/.rye
/home/xyc/.rye
├── env
├── pip-tools
│   ├── [email protected]
│   └── [email protected]
├── py
│   ├── [email protected]
│   ├── [email protected]
│   └── [email protected]
├── self
│   ├── bin
│   ├── include
│   ├── lib
│   ├── lib64 -> lib
│   ├── 
│   └── 
└── shims
    ├── python
    ├── python3
    └── rye
13 directories, 6 files

Rye is written in rust, but it takes advantage of python capabilities. In this articlePhilosophy - Rye ()In, the author says that rye does not use the system installed python. The self file here is rye directly fromGitHub - indygreg/python-build-standalone: Produce redistributable builds of PythonDownloaded built python.

Basic usage method

Basics - Rye ()Generally, just learn

  • rye init <PROJECT_NAME>
  • rye pin <PYTHON_VERSION>
  • rye add <PACKAGE_NAME>
  • rye sync

How Rye’s basic usage works

rye init

There is nothing to explain this. There is a template in the source code of rye. rye gets some information, then fill it in the template, and generates relevant files.

rye pin

Write the version number in the .python-service file. As for what .python-version is useful, I will talk about it later in the article.

rye add

There is a script template in the source code of rye

const PACKAGE_FINDER_SCRIPT: &amp;str = r#"
...Omit some code
finder = PackageFinder(
    index_urls=sources["index_urls"],
    find_links=sources["find_links"],
    trusted_hosts=sources["trusted_hosts"],
)
...Omit some code
print(([x.as_json() for x in choices]))
"#;

When running rye add, rye fills the corresponding parameters into this code template, then starts a python interpreter and runs the py code. If the dependency name provided by the user is correct, fill in the dependency.

➜  learn_rye git:(master) ✗ rye add typer
Added typer>=0.9.0 as regular dependency

rye sync

This command is critical. Because rye add just fills in the dependency and will not really install the dependency. rye sync will update the virtual environment based on the content. If there is no virtual environment yet, one will be created. The key steps of rye sync are as follows

  • Update lock file (the principle will not be explained in detail here)
  • If rye uses python itself and has not installed pip-tools yet, install it.
  • Pass the corresponding parameters to the pip-sync command and run it

The above is the core working principle of rye.

How other ways of using rye work.

Shims - Rye ()rye supportpython +3.8 This way to run any version of the python interpreter.

➜  ~ python +3.10 --version
Python 3.10.11
➜  ~ python +3.11 --version
Python 3.11.3

What is the principle?

As mentioned earlier, after installing rye, you need to configure the shell so that in the shell,$HOME/.rye/shimsAt the front of PATH. In this way, 'python' points to the actual python interpreter, but the front of PATH. In this way, `python` points to the actual python interpreter, but the front of `PATH. In this way, 'python' points to not the actual python interpreter, but 'HOME/.rye/shims/python`.

➜  ~ which python
/home/xyc/.rye/shims/python

It forwards the command to the specified version of the python interpreter.

Summarize

This article briefly introduces the working principle of rye. While studying how it works, I looked at the source code of rye. It seems that the project is not big, it is worth learning.

This is the end of this article about the working principle of Rye, the Python project management tool. For more information about the working principle of Rye, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!