Skip to content

Instantly share code, notes, and snippets.

@simonmeggle
Last active July 8, 2022 13:21
Show Gist options
  • Save simonmeggle/affd7409e76656f9857a80f22f1f799b to your computer and use it in GitHub Desktop.
Save simonmeggle/affd7409e76656f9857a80f22f1f799b to your computer and use it in GitHub Desktop.
Demo script to prove that without VIRTUAL_ENV, Poetry won't use venvs from pyenv-virtualenv
#!/bin/bash
set -u -e -o pipefail
#title : pyenv-poetry-test.sh
#description : A simple script which I wrote to discover what information Poetry is
# : relying on when it should use the virtualenv created by pyenv.
# : See https://github.com/python-poetry/poetry/issues/5956
# : and https://github.com/pyenv/pyenv-virtualenv/issues/348
#author : Simon Meggle, <[email protected]>
#date : 20220608
#version : 0.1
#usage : bash pyenv-poetry-test.sh create
#==============================================================================
export PROJECT_DIR="project"
export PYENV_VENV_NAME="$PROJECT_DIR"
export ROOT_DIR="$(pwd)"
export PVERSION="3.8.13"
function create() {
log "Creating and entering $PROJECT_DIR"
mkdir -p $PROJECT_DIR && pushd $PROJECT_DIR > /dev/null
log "pyenv - create $PYENV_VENV_NAME: 'pyenv virtualenv $PVERSION $PYENV_VENV_NAME'"
pyenv virtualenv "$PVERSION" "$PYENV_VENV_NAME"
log "pyenv - set local version: 'pyenv local $PYENV_VENV_NAME'"
pyenv local "$PYENV_VENV_NAME"
log "pyenv - show local version: 'pyenv local'"
pyenv local
log "poetry - config: "
poetry config --list
log "poetry - initialization: 'poetry init -n --name=$PROJECT_DIR --python='^3.8' --dependency=robotframework'"
poetry init -n --name=$PROJECT_DIR --python='^3.8' --dependency=robotframework
log "poetry - install in pyenv: 'pyenv exec poetry install'"
# ### WITHOUT THIS VARIABLE, POETRY WON'T USE THE VIRTUALENV VERSION SET FOR THIS FOLDER ###
# (it will prepare the venv in the "poetry way")
export VIRTUAL_ENV=$(pyenv virtualenv-prefix)/envs/$PYENV_VENV_NAME
poetry install -vv
log "Pip list:"
pip list
}
function tidy() {
log "Tidying up"
remove_project
remove_venv
echo "-----"
}
function remove_project() {
log "Removing project"
cd $ROOT_DIR
log "Executing: rm -rf $PROJECT_DIR"
set +e
rm -rf $PROJECT_DIR
set -e
}
function remove_venv() {
log "Deleting pyenv-venv $PYENV_VENV_NAME"
log "Executing: pyenv virtualenv-delete -f $PYENV_VENV_NAME"
set +e
pyenv virtualenv-delete -f $PYENV_VENV_NAME
set -e
}
function main() {
log "PWD = $PWD"
log "Versions:"
log " pyenv: $(pyenv --version)"
log " poetry: $(poetry --version)"
log " pyenv global: $(pyenv global)"
log "---"
if [[ "$1" == "create" ]]; then
tidy
create
elif [[ "$1" == "tidy" ]]; then
tidy
else
echo "Usage: $0 [create|tidy]"
fi
}
function log {
echo "❯ $1"
}
print_env() {
printenv| grep -v "p9k" | egrep "VIRT|PYT|ENV" | sort
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment