· coding · 2 min read
Pyenv and Poetry: Setting Up a Professional Python Development Environment

Pyenv for managing multiple Python interpreters
The pyenv repo contains usage and installation instructions. If you’re on MacOS or a common Linux distro, you can install pyenv with brew.
brew install pyenvYou’ll then need to add the following snippet to your shell config, e.g. your .bash_profile, .bashrc, or .zshrc.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv init --path)"After using source on your config or restarting the shell, you should have the pyenv root command.
The command use to install any version of python is pyenv install. Display additional info for this command with pyenv install --help.
pyenv install 3.9.13 # exampleOnce you have a version installed, you can print out the versions on your machine with:
pyenv versions# example output
system
* 3.9.13 (set by /home/realu/.python-version)
3.10.4In this example, I have 2 different interpreters installed on my machine. The one with the * is currently set as my global interpreter. This is set manually using the pyenv global command.
pyenv global 3.10.4 # switches the global interpreter to 3.10.4You can verify this works as expected using python --version. You may be familiar with using python3 as the command instead of python. With pyenv, this is not necessary.
Reference: malexer/cheatsheets/pyenv.md
Poetry for dependency management and publishing packages
Reference: Poetry docs
Poetry can be installed with both curl and pip. I recommended using curl so that it will be global to your machine.
NOTE I highly, highly, highly recommend that you DO NOT use brew to install poetry. If you use brew, it’s going to install directly to your system, which prevents you from being able to leverage pyenv to seamlessly switch between Python interpreters.
# installation with pip: recommended option in tandem with pyenv
pip install poetry# For UNIX systems - installation with curl
curl -sSL https://install.python-poetry.org/ | python -After this installation command, add the poetry binary to the path in your shell config.
export PATH=$PATH:$HOME/.poetry/binUsing poetry
To create a project from scratch, call poetry new [pkg-name]
To create a project from a pre-existing codebase, cd to the root where the package is located (the parent repo or directory to the package) and call:
poetry initBoth of these commands will prompt you to interactively create a pyproject.toml.
After confirming the generation of the project, you can find all of the options you specified inside the pyproject.toml. You can now install the packages.
poetry installThis will resolve dependencies between each of the project’s packages and install them into a virtual environment.


