Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. How to Build and Publish Python Packages With Poetry

How to Build and Publish Python Packages With Poetry

Author image

Mayank Jain

Software Developer

Published on Mon Feb 26 2024

Building and publishing Python packages with Poetry involves a series of straightforward steps that enhance project management and dependency resolution. Poetry is a tool for Python package and dependency management that simplifies the process of packaging, dependency management, and publishing to the Python Package Index (PyPI). By utilizing Poetry, developers can easily specify dependencies, manage environments, and ensure that their packages are built and published with consistency and reliability. This guide will introduce the fundamental concepts of Poetry and walk through the process of preparing, building, and publishing a Python package.

What is Poetry?

Poetry is a tool for Python package and dependency management. It simplifies the process of defining project environments and dependencies through an easy-to-use command-line interface. With Poetry, developers can automatically manage their virtual environments, streamline package installation, and ensure consistent package builds. It uses the pyproject.toml file to declare project dependencies and metadata, making it straightforward to build, package, and publish projects to the Python Package Index (PyPI). Poetry's dependency resolution is precise and fast, resolving conflicts efficiently to create reproducible builds. This tool is designed to improve the workflow for Python developers, making it easier to distribute and install software.

How to Set Up the Project

Setting up a project with Poetry involves installing Poetry itself, creating a new project, establishing a virtual environment, and configuring project dependencies. This structured approach ensures that your development environment and package management are streamlined and efficient.

How to Install Poetry

To install Poetry, run the following command in your terminal:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

This command downloads and executes the Poetry installation script. Once installed, Poetry provides a command-line interface to manage Python packages and dependencies.

How to Create a New Project

Creating a new project with Poetry is straightforward. Use the command:

poetry new my_project

This command creates a new directory called my_project with a basic project structure, including the pyproject.toml file, which is crucial for project configuration.

Understanding the pyproject.toml File

The pyproject.toml file is the backbone of a Poetry-managed project. It contains metadata about your project and lists its dependencies. Here's a simple example:

[tool.poetry]
name = "my_project"
version = "0.1.0"
description = "A simple Poetry project."
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

This file specifies the project name, version, description, authors, Python version compatibility, and project dependencies.

How to Create a New Virtual Environment

Poetry automatically manages virtual environments. To create a new virtual environment for your project, simply run:

poetry install

This command reads the pyproject.toml file, installs the listed dependencies into a new virtual environment, and ensures your project is isolated from other Python projects.

How to Configure Project Dependencies

Adding dependencies to your project is done with the add command. For example, to add the requests library as a dependency, you would run:

poetry add requests

Poetry updates the pyproject.toml file and installs the requests package along with its dependencies into the virtual environment.

The poetry.lock File

The poetry.lock file is generated by Poetry to lock the versions of the packages installed, ensuring consistent builds. This file tracks the exact versions of each dependency (and sub-dependency) installed, to make sure every installation of the project on any machine uses the same versions, thus avoiding inconsistencies and conflicts.

By following these steps, you set a solid foundation for your Python project using Poetry, ensuring that it is well-organized, easily managed, and consistently reproducible across different environments.

How to Develop the Package

Developing the package with Poetry involves writing your package code, creating tests to ensure reliability, and managing test dependencies. This process ensures that your package is robust, functional, and ready for publication.

Example Usage of PhoneNumberValidator Class (Optional)

Suppose your package includes a PhoneNumberValidator class. Here's how it might be used:

from my_package import PhoneNumberValidator

validator = PhoneNumberValidator()

if validator.validate("+1234567890"):
    print("Valid phone number.")
else:
    print("Invalid phone number.")

This example demonstrates creating an instance of the PhoneNumberValidator class and using it to validate a phone number.

How to Test the Package

Testing your package is crucial to ensure its reliability and functionality. Poetry makes it easy to run tests using the poetry run command. For example, to run tests with pytest, you would use:

poetry run pytest

This command executes pytest within the virtual environment managed by Poetry, ensuring that tests run with the package's dependencies.

How to Install Test Dependencies

To add test dependencies, use the add command with the -D flag, which signifies that the dependency is for development only. For example, to add pytest as a test dependency, run:

poetry add -D pytest

Poetry adds pytest to the dev-dependencies section of the pyproject.toml file and installs it in the virtual environment.

How to Write the Tests

Tests should be written in a directory named tests within your project structure. Here's an example of a simple test for the PhoneNumberValidator class:

from my_package import PhoneNumberValidator

def test_phone_number_validator():
    validator = PhoneNumberValidator()
    assert validator.validate("+1234567890") == True
    assert validator.validate("invalid") == False

This test checks that the validate method returns True for a valid phone number and False for an invalid one. You can run this test using the poetry run pytest command, as mentioned earlier.

By following these steps, you ensure that your package is not only functional but also reliable and ready for use by others. Writing tests and managing dependencies are essential parts of developing a robust Python package with Poetry.

How to Publish the Package

Publishing a package with Poetry is a streamlined process that involves building the package and then uploading it to the Python Package Index (PyPI). This ensures that your package can be easily installed by others using pip. Below are the steps to publish your package.

First, configure your PyPI credentials. Poetry uses the pypi-token command to associate your account with a PyPI token:

poetry config pypi-token.pypi YOUR_PYPI_TOKEN

Replace YOUR_PYPI_TOKEN with your actual PyPI token. This step is necessary only once per machine, as Poetry stores your credentials securely.

Next, build your package. Poetry compiles your project into a distributable format (wheel and source distribution) with the following command:

poetry build

This command generates the files needed for distribution in the dist/ directory of your project.

Finally, publish your package to PyPI using:

poetry publish

This command uploads the package files from the dist/ directory to PyPI. If you wish to publish to a different repository, you can specify it with the --repository option.

By following these steps, your package will be available on PyPI for anyone to install with pip. Publishing with Poetry ensures that your package distribution is consistent and reliable, making it accessible to a wide Python community.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.