averyfreeman / rye-venv

A shell plugin for oh-my-zsh and Rye
0 stars 0 forks source link

zsh plugin: rye-venv

location-aware environment activation

Example with oh-my-zsh using powerlevel10k prompt w/ virtualenv and pyenv right-side

Before entering rye project folder:

╭─    ~/containers ────────────────────────────────────────────────────────────╮
╰─                                                              at 12:33:24 PM  ─╯
❯ which python; python -V
    /usr/bin/python
    Python 3.12.3

After entering rye project folder:

❯ cd djtoolbar

╭─    ~/containers/djtoolbar  on   main ?13 ──────── django  ╱ 3.12.2   ─╮
╰─                                                              at 11:43:54 AM  ─╯
❯ which python; python -V
    /home/avery/containers/djtoolbar/.venv/bin/python
    Python 3.12.2

Leaving the rye project folder again:

❯ cd ..

╭─    ~/containers ────────────────────────────────────────────────────────────╮
╰─                                                              at 12:33:24 PM  ─╯
❯ which python; python -V
    /usr/bin/python
    Python 3.12.3

What is Rye?

Register article from 2023

Recent discussion on Hacker News


rye plugin, inspired by poetry plugin oh-my-zsh has been entirely rewritten, and renamed rye-venv

Why use this plugin with rye?

The original source of inspiration poetry-envsource

[{ "paraphrase": ["poetry-env", "README.md"] }]

This plugin automatically changes rye environment when you enter a rye project directory. It looks for pyproject.toml file to determine if its a rye environment. If it fings pyproject.toml, and a venv is defined by rye, it'll try and source the activate file, enabling the virtualenv automatically. When you leave the folder, it will deactivate it.

Note: rye also uses venv and pyenv-like file .python-version, so if you primarily use rye, I recommend removing those plugins from your .zshrc array, as they'll likely conflict.

The python omz plugin could still useful, but I recommend running it after rye-venv plugin, and disabling PYTHON_AUTO_VRUN (set to 0) Oh-my-ZSH Python plugin info https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/python

At some point I will have to deconflict this plugin with poetry, but since poetry doesn't manage Python versions, I am not sure why someone would use it instead of rye.


Still need to install rye?

Official rye installation instructions

Rye releases on github

Arch Linux official

Homebrew (Mac and linux)

Mac Ports (MacOS)

Scoop (Windows)

Winget (Windows)

Requirements:


zsh shell capable of: add-zsh-hook autoload chpwd

Optional (but recommended): oh my zsh

Usage:

With oh-my-zsh:

tested w/ Arch linux official rye package, global config not sourced

  1. clone this repo to $ZSH/custom/plugins/rye-venv
    ╰─❯ cd $ZSH/custom/plugins
    clone --depth 1 https://github.com/averyfreeman/rye-venv.git
  2. add rye-venv to the plugins array in $HOME/.zshrc.
    # $HOME/.zshrc
    # note: other plugins included to demonstrate order, but are not necessary
    plugins=(asdf ... rye-venv python)
  3. reload .zshrc
    ╰─❯ zsh  # or open a new shell

without oh-my-zsh:

this method is untested if someone wants to try it, please send me issue w/ results in theory, it should just need the 3 requirements listed above

  1. clone repo somewhere
  2. source plugin file
  3. reload .zshrc
    ╰─❯ cd /your/parent/folder
    clone --depth 1 https://github.com/averyfreeman/rye-venv.git
    echo 'source /path/to/rye-venv/rye-venv.plugin.zsh' >> $HOME/.zshrc
    zsh

Example:


╰─❯ cd $ZSH/custom/plugins
    clone https://github.com/averyfreeman/rye-venv.git
    vim $HOME/.zshrc
    /plugins

find uncommented line:

plugins=(asdf ... python)

add rye-venv here:

# note: other plugins shown not necessary
#   included to demonstrate order
    plugins=(asdf ... rye-venv python)

save file, exit, and reload .zshrc - one method:

╰─❯ zsh
    echo $plugins

should show:

    ... rye-venv ...

Also, don't forget:

$RYE_HOME:

╰─❯ env | grep RYE
    RYE_HOME=/home/$USER/.rye

20240618 Feel free to contact me if you have any questions