Skip to content

Instantly share code, notes, and snippets.

@ulope
Created April 13, 2019 15:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulope/244eb62b52c59547500fb967c464107b to your computer and use it in GitHub Desktop.
Save ulope/244eb62b52c59547500fb967c464107b to your computer and use it in GitHub Desktop.
Poetry Python version wrapper (ZSH)
# This helps run a globally installed poetry with specific versions of python.
# When run from inside a poetry project the correct version is automatically
# picked up from pyproject.toml.
# Outside of a project (e.g. `poetry new`) a specific version can be chosen by
# setting a `PYTHON` env variable.
# e.g.:
#
# PYTHON=3.7 poetry new someproject
poetry() {
if [[ -f pyproject.toml ]]; then
PYVER=$(grep -E '^python =' pyproject.toml | sed -E 's/^python = "\^([0-9].[0-9])"/\1/')
python${PYVER} $(whence -p poetry) "$@"
else
if [[ -v PYTHON ]]; then
python${PYTHON} $(whence -p poetry) "$@"
else
$(whence -p poetry) "$@"
fi
fi
}
@jimrthy
Copy link

jimrthy commented Apr 23, 2019

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment