Python > Deployment and Distribution > Dependency Management > Creating `requirements.txt` or `pyproject.toml` for dependencies

Creating `pyproject.toml` using Poetry

This snippet demonstrates how to initialize a `pyproject.toml` file using Poetry, a modern Python dependency management and packaging tool. `pyproject.toml` is a standardized configuration file for Python projects, as specified by PEP 518. Poetry offers features like dependency resolution, virtual environment management, and packaging, making it easier to manage complex projects.

Concepts Behind `pyproject.toml` and Poetry

pyproject.toml is a TOML (Tom's Obvious, Minimal Language) file that serves as a central configuration file for Python projects. It replaces older methods like setup.py and requirements.txt for managing dependencies, build systems, and other project-related settings. Poetry is a tool that simplifies the process of managing dependencies and packaging Python projects using pyproject.toml. It handles virtual environments, dependency resolution, and publishing packages.

Code Snippet

This example demonstrates the basic steps to initialize and use Poetry. First, ensure that Poetry is installed. Then, navigate to your project directory in the terminal and run `poetry init`. Poetry will guide you through a series of prompts to configure your project's metadata and dependencies. The poetry add command is used to add new dependencies to your project. Finally, poetry install resolves and installs all the dependencies specified in the pyproject.toml file within a virtual environment managed by Poetry.

# Install Poetry (if not already installed)
# pip install poetry

# Navigate to your project directory in the terminal
# cd your_project_directory

# Initialize a new pyproject.toml file using Poetry
# poetry init

# Follow the prompts to configure your project.  Poetry will ask you
# questions about the project name, version, description, author, etc.
# It will also ask you to specify dependencies.

# Example pyproject.toml (after initialization and adding dependencies)
# [tool.poetry]
# name = "my-project"
# version = "0.1.0"
# description = ""
# authors = ["Your Name <your.email@example.com>"]
# readme = "README.md"

# [tool.poetry.dependencies]
# python = ">=3.7,<3.12"
# requests = "^2.28.1"

# [build-system]
# requires = ["poetry-core"]
# build-backend = "poetry.core.masonry.api"


# Add a dependency using Poetry
# poetry add <package_name>
# Example: poetry add requests


# Install all dependencies defined in pyproject.toml
# poetry install

Explanation

1. **Poetry Installation:** The snippet assumes you have Poetry installed. If not, you can install it using `pip install poetry`. 2. **Project Initialization:** The `poetry init` command starts the process of creating a `pyproject.toml` file. It will ask interactive questions and generate a `pyproject.toml` file based on the provided information. 3. **Dependencies:** You can specify dependencies during the `poetry init` process or add them later using `poetry add `. Poetry handles dependency versioning and ensures compatibility. 4. **`poetry install`**: This command creates a virtual environment (if one doesn't already exist) and installs all the dependencies listed in your pyproject.toml and poetry.lock (if it exists) file. 5. **`poetry.lock`**: This file records the exact versions of all dependencies (including transitive dependencies). This makes builds repeatable and reliable.

Real-Life Use Case

Consider a data science project that relies on specific versions of libraries like NumPy, Pandas, and Scikit-learn. Poetry and pyproject.toml ensure that everyone working on the project uses the same versions of these libraries, preventing compatibility issues and ensuring consistent results.

Best Practices

  • Use version constraints: Specify version ranges for your dependencies to allow for updates while maintaining compatibility.
  • Commit both `pyproject.toml` and `poetry.lock`: The `poetry.lock` file ensures reproducibility.
  • Regularly update dependencies: Use `poetry update` to update your dependencies to the latest compatible versions.
  • Use poetry's virtual environment management: Poetry automatically manages virtual environments for your projects.

Alternatives

Alternatives to Poetry include `pip-tools`, `conda`, and `PDM` (Python Dependency Manager). Each tool has its own strengths and weaknesses, so choose the one that best suits your project's needs.

Pros

  • Modern and feature-rich dependency management.
  • Handles virtual environment management.
  • Simplifies packaging and publishing.
  • Dependency locking for reproducibility.

Cons

  • Requires learning a new tool and workflow.
  • Can be overkill for very simple projects.
  • Relatively new compared to pip, although adoption is increasing rapidly.

FAQ

  • How do I specify a specific version of a dependency?

    Use the `=` operator in the `pyproject.toml` file. For example, `requests = "==2.28.1"`.
  • What is the `poetry.lock` file?

    The `poetry.lock` file contains the exact versions of all dependencies, including transitive dependencies. It ensures that everyone working on the project uses the same versions of the dependencies, guaranteeing reproducibility.
  • How do I activate the virtual environment managed by Poetry?

    Poetry automatically activates the virtual environment when you run Poetry commands within the project directory. You can also manually activate it using `poetry shell`.