DEV Community

Cover image for PyEnv & Poetry - BFFs ๐Ÿ’–
Matthew Cale
Matthew Cale

Posted on

PyEnv & Poetry - BFFs ๐Ÿ’–

What is this?

This is a resource made, admittedly mostly for myself, that will allow me to recall a method of Python dev environment management I don't dislike. It covers the use of two distinct but important Python environment helpers:

  • PyEnv (Docs)
    • Uses path shims to help you manage several versions of Python simultaneously.
  • Poetry (Docs)
    • A dependency manager that feels similar in my approximation to a tool like yarn or npm from the Node world, and automates certain tasks (venv creation and activation for example).

Why make this / why read this?

  • You find that managing Python projects in a clean, repeatable, and predictable way is less simple (or less fun) than you have found the process to be in other languages.

How do I use / read this?

This is a technical guide not an in depth explanation (though that may be forthcoming) and so I expect this guide to be used primarily as a way to kick start your memory on how to set up Python projects in a way you don't dislike. Best of luck ๐Ÿ€


Pre-Reqs ๐Ÿซธ

These steps are to be done once per machine and if you've already accomplished these steps you may skip this section.

Install PyEnv & A Modern Python Version ๐Ÿ

brew update
brew install pyenv
pyenv --version
# pyenv 2.3.15

pyenv install 3.10.7
# Wait for a bit for install to finish...

pyenv shell 3.10.7
python --version
# Python 3.10.7 ๐ŸŽ‰
Enter fullscreen mode Exit fullscreen mode

Optional: Set A System Wide Python Version

pyenv global 3.10.7
Enter fullscreen mode Exit fullscreen mode

Install Poetry โœ๏ธ

curl -sSL https://install.python-poetry.org | python3 -
poetry --version
Enter fullscreen mode Exit fullscreen mode

Optional: Tell poetry to create virtual environments in the current project directory

poetry config virtualenvs.in-project true
poetry config virtualenvs.in-project
# true
Enter fullscreen mode Exit fullscreen mode

Steps ๐Ÿงญ

  • Make a new project directory
cd Desktop/Code/personal/python
mkdir planet-express-api
cd planet-express-api 
Enter fullscreen mode Exit fullscreen mode
  • Set a Python version for this project directory
pyenv local 3.10.7
python --version 
# 3.10.7
cat .python-version
# 3.10.7
Enter fullscreen mode Exit fullscreen mode

Note: This command will create a file that pyenv looks for in the current working directory (the folder you are in) whose content will tell pyenv which Python version to use.

  • Create a poetry project
# Leave off the -n flag if you wish to add precise data 
# to the pyproject.toml file poetry creates.
poetry init -n
ls | grep pyproject.toml
# pyproject.toml
Enter fullscreen mode Exit fullscreen mode
  • Initialize and start your Virtual Environment using poetry
poetry shell
# Creating virtualenv planet-express-api
# bash -c planet-express-api/.venv/bin/activate
Enter fullscreen mode Exit fullscreen mode
  • Test it by adding a dependency
# Add a dependency
poetry add pendulum 

# Start a Python REPL
python 

# Use the dependency
import pendulum
now = pendulum.now("Europe/Paris")
now.to_iso8601_string()
# '2023-05-27T19:40:17.452958+02:00'
# Use `ctrl + d` to exit the REPL 
Enter fullscreen mode Exit fullscreen mode
  • Stop using the virtual environment
# Exit the virtual environment
deactivate

# Start a Python REPL
python

# Validate the dependency is not available
import pendulum
ModuleNotFoundError: No module named 'pendulum' 
Enter fullscreen mode Exit fullscreen mode

Note: To get back into the virtual environment just use poetry shell again.


Summary

In this article you have used PyEnv and Poetry together to create a project in a desired version of Python (pyenv) whose dependencies and virtual environment are managed by a single robust tool (poetry) cheers to you ๐Ÿป

Top comments (2)

Collapse
 
scooterx3 profile image
Riley

When you do this do you ever get a problem with Poetry's shell not respecting the pyenv version? I tested, and the first directory I created the .python-version file in (using pyenv local 3.7.17) was fine, but then I poetry init and poetry shell and it gives me python 3.10.6, my system version.

I wound up using the solution from the very last post of this github issue. Upon setting that poetry config item, it then respected the version.

If it matters, I installed Poetry using pipx.

Collapse
 
mattcale profile image
Matthew Cale

Couple of things:

  • Sorry I didn't see this comment for so long!
  • Thanks for sharing this information!
  • For folks seeing this comment and having a similar problem please see the linked issue as it directly addresses the concern mentioned if I understand properly.
  • In answer to your question: No I never ran into this.
  • However, I did NOT install Poetry via PIPX, but using the install script on the Poetry website.

python-poetry.org/docs/#installation

github.com/python-poetry/poetry/is...