Hygeia: Managing Python toolchains with Rust
Tags: python, rust, linux, tutorial
Published: 2021-04-08 02:33:32

If you're a python dev, you may be familiar with the use of [pyenv](https://github.com/pyenv/pyenv) for managing the interpreters available on your system.

pyenv is written in Bash, that's why it only works on Linux or macOS. For Windows users there's a fork named [pyenv-win](https://github.com/pyenv-win/pyenv-win).

[Hygeia](https://github.com/nbigaouette/hygeia), a Python interpreter manager, built with Rust, works similar to pyenv, but aims to be portable across Windows, Linux and macOS.

Note: If you're using a tool like `Poetry` or `pipenv` for managing the dependencies of your project, install it before Hygeia.

# Installation
The `Requirements` section in the `README.md` file in the repository contains instructions for installing dependencies of Hygeia on macOS and Linux. On Windows, just download the latest version from the [release page](https://github.com/nbigaouette/hygeia/releases).

If you're using Linux, check [pyenv's wiki](https://github.com/pyenv/pyenv/wiki#suggested-build-environment), as dependencies are the same.

After installing dependencies, download and extract Hygeia installer. Run the following commands in the Linux terminal:

```sh
$ curl -s https://api.github.com/repos/nbigaouette/hygeia/releases/latest \
  | grep "browser_download_url.*linux" \
  | cut -d : -f 2,3 \
  | tr -d \" \
  | wget -qi - -O hygeia.zip

$ unzip hygeia.zip
```

Where<sup>[1](https://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8)</sup>:

- Use `curl` to get the JSON response for the latest release
- Use `grep` to find the line containing file URL
- Use `cut` and `tr` to extract the URL
- Use `wget` to download it

Once you download and extract Hygeia, run:

```sh
$ ./hygeia setup <SHELL>
```

Replacing `<SHELL>` with `bash`, `zsh` or `powershell`.

Hygeia is set up and ready to use it. Now you can remove the files downloaded.

```sh
$ rm hygeia*
```

## Basic Commands
To see the commands available, just type `hygeia --help`.

```sh
$ hygeia --help

hygeia v0.3.7 (712c5f2a5 2021-03-27)
Control which Python toolchain to use on a directory basis

USAGE:
    hygeia [FLAGS] [SUBCOMMAND]

FLAGS:
    -h, --help       
            Prints help information

    -V, --version    
            Prints version information

    -v, --verbose    
            Verbose mode (-v, -vv, -vvv, etc.)


SUBCOMMANDS:
    help       Prints this message or the help of the given subcommand(s)
    install    Install version, either from the provided version or from '.python-version'
    list       List installed Python versions
    path       Get path to active interpreter
    run        Run a binary from the installed '.python-version'
    select     Select specified Python versions to use
    setup      Setup the shim
    update     Update pycors to latest version
    version    Get version of active interpreter
```

`hygeia list` will display the interpreters available on your system.

```sh
$ hygeia list

+--------+---------+---------------------+---------------------------------------------------+
| Active | Version | Installed by hygeia | Location                                          |
+--------+---------+---------------------+---------------------------------------------------+
|        |  3.9.2  |                     | /usr/bin                                          |
+--------+---------+---------------------+---------------------------------------------------+
|        |  3.7.6  |          ✓          | /home/user/.hygeia/installed/cpython/3.7.6/bin    |
+--------+---------+---------------------+---------------------------------------------------+
```

For checking the Python version being used in the current directory, run:

```sh
$ hygeia version

3.9.2
```

For installing any version of Python, run:

```
$ hygeia install =3.7.6
```

This command will download and install Python `3.7.6`. It will create a new directory into `$HOME/.hygeia/installed/cpython`, named `3.7.6`. Any Python interpreter installed using `hygeia` is built with `--enable-shared`.

To set the active Python interpreter, type `hygeia select =3.7.6` in the directory of your project.

To uninstall a specific Python toolchain, just remove the directory from `$HOME/.hygeia/installed/cpython`.

## Poetry support
Create a new project with Poetry:

```sh
$ poetry new project_name
```

Change to the project directory:

```sh
$ cd project_name
```

Install and set the Python interpreter:

```sh
$ hygeia install =3.7.6
$ hygeia select =3.7.6
```

Tell Poetry what version of Python to use:

```sh
$ poetry env use $HOME/.hygeia/installed/cpython/3.7.6/bin/python
```

If you're using Hygeia along with Poetry, you must specify the full path where the Python binary is stored.

Install dependencies of your project:

```sh
$ poetry install
```

Your environment is ready and you can start writing code.