Introduction to Poetry
Poetry is a powerful tool for Python developers that simplifies project management, dependency handling, and package distribution. It aims to solve common pain points in the Python ecosystem by providing a single, intuitive interface for managing your projects from start to finish.
Why Use Poetry?
- Simplified dependency management: Poetry uses a
pyproject.toml
file to declare dependencies, making it easy to specify and lock versions. - Built-in virtual environment handling: No need for separate virtualenv commands; Poetry manages environments for you.
- Streamlined packaging and publishing: Poetry simplifies the process of building and publishing packages to PyPI.
- Consistent project structure: Poetry encourages a standardized project layout, improving code organization.
Getting Started with Poetry
Installation
To install Poetry, run the following command in your terminal:
curl -sSL https://install.python-poetry.org | python3 -
Verify the installation by running:
poetry --version
Creating a New Project
To create a new Python project with Poetry, use the new
command:
poetry new my-awesome-project
This creates a new directory with the following structure:
my-awesome-project/
├── pyproject.toml
├── README.md
├── my_awesome_project/
│ └── __init__.py
└── tests/
└── __init__.py
Converting an Existing Project
To add Poetry to an existing project, navigate to your project directory and run:
poetry init
This will guide you through creating a pyproject.toml
file interactively.
Managing Dependencies
Adding Dependencies
To add a new dependency to your project:
poetry add requests
This command adds the package to your pyproject.toml
file and installs it in your virtual environment.
Specifying Version Constraints
Poetry allows you to specify version constraints for your dependencies:
[tool.poetry.dependencies] python = "^3.8" requests = "^2.25.1" numpy = ">=1.20,<2.0"
Development Dependencies
For packages only needed during development, use the --dev
flag:
poetry add pytest --dev
This adds the package to the [tool.poetry.dev-dependencies]
section in pyproject.toml
.
Working with Virtual Environments
Poetry automatically creates and manages virtual environments for your projects.
Activating the Virtual Environment
To activate the virtual environment:
poetry shell
Running Commands in the Virtual Environment
To run a command without activating the environment:
poetry run python your_script.py
Building and Publishing Packages
Building Your Package
To build your package:
poetry build
This creates both source and wheel distributions in the dist/
directory.
Publishing to PyPI
First, configure your PyPI credentials:
poetry config pypi-token.pypi your-token
Then, publish your package:
poetry publish
Advanced Poetry Features
Custom Package Sources
You can specify custom package sources in your pyproject.toml
:
[[tool.poetry.source]] name = "private" url = "https://private.pypi.org/simple/"
Scripts and Entry Points
Define custom scripts in your pyproject.toml
:
[tool.poetry.scripts] my-command = "my_package.cli:main"
Exporting Requirements
Generate a requirements.txt
file:
poetry export -f requirements.txt --output requirements.txt
Best Practices and Tips
- Keep your
pyproject.toml
clean: Regularly review and remove unused dependencies. - Use Poetry's lock file: Commit
poetry.lock
to ensure reproducible builds. - Leverage Poetry's plugin ecosystem: Explore plugins for additional functionality.
- Use Poetry's build isolation: It helps prevent contamination from system-wide packages.
By embracing Poetry in your Python projects, you'll streamline your workflow, improve dependency management, and simplify the packaging and distribution process. As you become more familiar with Poetry's features, you'll find it an invaluable tool in your Python development toolkit.