A workflow for using pyenv and poetry together in your project

If you really want to use poetry and pyenv for your python project instead of your tutorial's method, try using this workflow.

By Ace Z. Alba

Published on March 8, 2023, 2:44 pm

Category: cheatsheet

Tag: python, project management, poetry, pyenv

Poetry is a modern project dependency manager for Python. Pyenv is a Ruby-inspired version manager for Python. Both tools aim to solve two of the major painpoints of Python for beginners and professionals alike. However, many tutorials online have their own particular workflow for virtual environments, project dependencies, and Python versions. So if you want to force through using poetry and pyenv instead, try using the commands below to set up your project directory before going forward with the tutorial.

The workflow below assumes that you already have Poetry and Pyenv installed on your system. Make sure that you can run both on your system before going through with this workflow. All commands are run in Linux Mint 20.3 using Poetry v1.3.2 and Pyenv v2.3.11.

# Install a specific version of python using pyenv
pyenv install 3.10.2

# Set a local python version 
pyenv local 3.10.2

# Run a pyenv shell for your specific version if in case the config below does not work
pyenv shell 3.10.2

# If using poetry for first time, run command to make poetry use pyenv
poetry config virtualenvs.prefer-active-python true

# if already have a folder, run to prepopulate with poetry
poetry init

# If project does not exists, use `new`
poetry new demo-app

# Once you have a pyproject.toml in your project directory, do this. This will create your virtual environment in your package folder
poetry install

# Activate your poetry-generated virtual environment
poetry shell

## Verify that poetry is using your preferred python version
python --version

# Exit the virtual environment without leaving the shell
exit

This workflow may be updated in the future in case new situations involving using poetry and pyenv come up. Hope it helps.