Skip to content

Python Project ‐ Poetry

Steve Gibbard edited this page Feb 27, 2025 · 1 revision

Prerequisites

The following assume you have pyenv and poetry installed

Start a new project with Poetry

Choose Python version

List available versions locally

% pyenv versions
  system
  3.10.13
* 3.11.8 (set by /Users/username/.pyenv/version)

List all available Python versions of 3.12

% pyenv install --list | grep 3.12
  3.12.0
  3.12-dev
  3.12.1
  3.12.2

Install a Python version

% pyenv install 3.12.2            
python-build: use openssl@3 from homebrew
python-build: use readline from homebrew
Downloading Python-3.12.2.tar.xz...

Set the local version for the project

% cd my_project
% pyenv local 3.12.2
% pyenv versions    
  system
  3.10.13
  3.11.8
* 3.12.2 (set by /Users/username/Development/ai_agent/.python-version)

Check Python version

% python --version
Python 3.12.2

Setup a Poetry Project

Init a Poetry Project

% poetry init

Alternatively create with defaults

% poetry new .

Configure Poetry for Python version

Use the pyenv Python version

% poetry env use $(pyenv which python)
Creating virtualenv ai-agent-wJEa74kU-py3.12 in /Users/username/Library/Caches/pypoetry/virtualenvs
Using virtualenv: /Users/username/Library/Caches/pypoetry/virtualenvs/ai-agent-wJEa74kU-py3.12

Check the virtual env is setup correctly

% poetry env info

Activate the virtual environment

Activate virtual env

% poetry shell

Run the project

Execute a python script using poetry

% poetry run python main.py

Add dependencies to a Poetry project

Add application dependencies

% poetry add requests

Add dev dependencies

% poetry add --dev pytest

Confirm Poetry and pyenv are working

Check

% poetry check
All set!

Clone this wiki locally