This document outlines the canonical workflow for setting up new Python projects to ensure consistency across all development work.
Follow these steps in order when starting a new Python project:
pyenv local <project-python>- Sets the Python version for the specific project
- Creates a
.python-versionfile in the project root - Ensures consistent Python version across environments
Choose one of the following based on your needs:
For existing projects:
poetry init- Interactive initialization of
pyproject.toml - Allows you to specify project metadata and dependencies
For new projects:
poetry new <project-name>- Creates a new project directory with standard structure
- Generates
pyproject.tomland basic project layout
poetry add <package-name>- Installs dependencies and adds them to
pyproject.toml - Poetry automatically creates and activates a
.venvvirtual environment - Updates
poetry.lockfile with exact dependency versions
Examples:
poetry add requests
poetry add pytest --group dev
poetry add black --group devUse one of these approaches to execute commands in the project environment:
Option A: Direct execution with poetry run
poetry run python script.py
poetry run pytest
poetry run black .Option B: Activate shell environment
poetry shell
# Now you're in the activated environment
python script.py
pytest
black .- Consistency: Same approach across all projects
- Isolation: Each project has its own virtual environment
- Reproducibility:
poetry.lockensures exact dependency versions - Simplicity: Poetry handles virtual environment creation and activation
- Modern tooling: Leverages current Python best practices
# Complete setup for new project
pyenv local 3.11.0
poetry init
poetry add requests pytest black --group dev
poetry shell
# Or for existing project
pyenv local 3.11.0
poetry install
poetry shellFollow this workflow consistently to maintain clean, reproducible Python development environments.