Utility scripts for a smooth Python experience with pyenv and Poetry
To use these commands, put this in your .zshrc
eval "$(pyenv init -)"
alias newpyenv='source new-pyenv-script'
alias alpe='source activate-local-pyenv-script'
alias vpoetry='alpe 2>>/dev/null || newpyenv && poetry'
Put the new-pyenv-script
and activate-local-pyenv-script
files on your PATH
-
alpe
- activate local pyenv (virtualenv); looks after a .python-version file in the current directory and, if found, activates it; I found that commands with thelocal
Python versions to be unstable, so always activating it is a good idea. -
newpyenv <name>
- creates a new pyenv-virtualenv with name or, if the parameter is missing, the name of the current directory, then it runspyenv local <name>
andpip update pip setuptools
-
vpoetry
- virtualenvpoetry; creates a new pyenv virtualenv with the name of the current directory and sets it as local (if there is not already a local virtualenv) and activates the localpyenv-virtualenv
, after which it runspoetry
with any arguments you give it.
Without the scripts, if you would setup a Poetry package with pyenv, you would type something like this:
mkdir {package-name} && cd {package-name}
pyenv virtualenv {current-python-version} {package-name}
pyenv activate {package-name}
python3 -m pip install -U pip setuptools
poetry init
With the scripts from this repo it becomes:
mkdir {package-name} && cd {package-name}
vpoetry init
pyenv
lets you easily switch between multiple versions of Python. It has the pyenv-virtualenv
plugin which manages virtual environments across all Python versions. This means that you will have all your virtual environments regardless of Python version in one place - try pyenv virtualenvs
. The alpe
and newpyenv
commands described in the section below are strictly for pyenv use. Here's a great pyenv resource.
Poetry
is a great tool for dependency management. It has a much better dependency resolver than pip and also offers a very friendly building and packaging solution for Python packages. It can save you lots of time on arcane errors. The problem with it is that it doesn't offer the same virtualenv experience as pyenv
and you can't access from pyenv
the virtual environments it creates. To get the best out of Poetry, use it together with pyenv. The vpoetry
command from the below section streamlines the setup.