Created
November 16, 2024 03:12
-
-
Save illuzian/a3a79c68a1e4ab810b400028e310ecea to your computer and use it in GitHub Desktop.
ZSH profile function to define a binary in its own pyenv virtualenv and setup if not already
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use as follows | |
# COMMAND=pyenv_venv_command command | |
# $COMMAND params | |
function pyenv_venv_command() { | |
COMMAND=$1 | |
if (( ! $+commands[pyenv] )); then | |
echo "no pyenv" 1>&2 | |
which $COMMAND | |
fi | |
PACKAGE=$1 | |
VIRTUALENV_NAME=$1 | |
if [[ -v 2 ]]; then | |
PACKAGE=$2 | |
fi | |
if [[ -v 3 ]]; then | |
VIRTUALENV_NAME=$3 | |
fi | |
if PYENV_VERSION=$VIRTUALENV_NAME pyenv which $COMMAND 2> /dev/null | grep '.pyenv/versions' > /dev/null 2&>1; then | |
PYENV_VERSION=$VIRTUALENV_NAME pyenv which $COMMAND | |
else | |
if [[ -v 4 ]]; then | |
PYENV_TARGET_VERSION=$4 | |
if ! pyenv versions | grep " ${PYENV_TARGET_VERSION} " &> /dev/null; then | |
pyenv install $PYENV_TARGET_VERSION | |
fi | |
PYENV_VERSION=$PYENV_TARGET_VERSION pyenv virtualenv $VIRTUALENV_NAME > /dev/null 2&>1 | |
else | |
pyenv virtualenv $VIRTUALENV_NAME > /dev/null 2&>1 | |
fi | |
PYENV_VERSION=$VIRTUALENV_NAME pyenv exec pip install $PACKAGE 1>&2 | |
PYENV_VERSION=$VIRTUALENV_NAME pyenv which $COMMAND | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment