DEV Community

Cover image for Poetry: Simplifying Python Dependency Management on Linux
Shanu Kumawat
Shanu Kumawat

Posted on

Poetry: Simplifying Python Dependency Management on Linux

The Need for Virtual Environments and Reproducibility

Python projects often rely on numerous external libraries and packages. As projects grow and evolve, managing these dependencies can become complex. Two critical aspects of Python development are:

  1. Virtual Environments: Isolated spaces that keep project dependencies separate from system-wide Python installations.

  2. Reproducibility: Ensuring that a project can be easily set up and run consistently across different machines or environments.

Traditional tools like venv and pip have long been used for these purposes, but they often require multiple steps and manual intervention. This is where Poetry comes in, offering a more streamlined and robust solution.

Why Choose Poetry?

Poetry offers several advantages over traditional tools:

  1. Simplified Workflow: Combines dependency management, packaging, and publishing in one tool.

  2. Dependency Resolution: Automatically resolves dependencies and potential conflicts.

  3. Reproducible Builds: Ensures consistent environments across different machines.

  4. Lock File: Generates a lock file for exact version control of all dependencies.

  5. Project Isolation: Creates and manages virtual environments automatically.

  6. Intuitive Commands: Offers a user-friendly CLI for common tasks.

Installing and Setting Up Poetry

curl -sSL https://install.python-poetry.org | python3 -
Enter fullscreen mode Exit fullscreen mode

After installation, add Poetry to your PATH by adding the following line to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc):

export PATH="$HOME/.local/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Restart your terminal or run source ~/.bashrc (or the appropriate file) to apply the changes.

Verify the installation by running:

poetry --version
Enter fullscreen mode Exit fullscreen mode

Enable tab completion for Bash, Fish, or Zsh

poetry supports generating completion scripts for Bash, Fish, and Zsh.

Bash

poetry completions bash >> ~/.bash_completion
Enter fullscreen mode Exit fullscreen mode

Fish

poetry completions fish > ~/.config/fish/completions/poetry.fish
Enter fullscreen mode Exit fullscreen mode

Zsh

poetry completions zsh > ~/.zfunc/_poetry
Enter fullscreen mode Exit fullscreen mode

Using Poetry

Creating a New Project

To create a new Python project with Poetry:

poetry new my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

This creates a new directory with a basic project structure, including a pyproject.toml file.

Adding Dependencies

To add a new dependency:

poetry add requests
Enter fullscreen mode Exit fullscreen mode

This adds the package to your pyproject.toml file and installs it in the virtual environment.

Managing Dependencies

View installed packages:

poetry show
Enter fullscreen mode Exit fullscreen mode

Update all packages:

poetry update
Enter fullscreen mode Exit fullscreen mode

Remove a package:

poetry remove requests
Enter fullscreen mode Exit fullscreen mode

Running Scripts

Execute Python scripts within the project's virtual environment:

poetry run python your_script.py
Enter fullscreen mode Exit fullscreen mode

Managing the Virtual Environment

Activate the virtual environment:

poetry shell
Enter fullscreen mode Exit fullscreen mode

Deactivate it:

exit
Enter fullscreen mode Exit fullscreen mode

Building and Publishing

Build your project:

poetry build
Enter fullscreen mode Exit fullscreen mode

Publish to PyPI:

poetry publish
Enter fullscreen mode Exit fullscreen mode

Exporting Requirements

Generate a requirements.txt file:

poetry export -f requirements.txt --output requirements.txt
Enter fullscreen mode Exit fullscreen mode

Conclusion

Poetry simplifies Python project management by providing a unified tool for dependency management, virtual environments, and packaging. Its intuitive interface and powerful features make it an excellent choice for Python developers looking to streamline their workflow and ensure project reproducibility.

Top comments (1)

Collapse
 
msc2020 profile image
msc2020

Good work! Thanks.