Last modified: May 28, 2025 By Alexander Williams

Install Python Package with Poetry

Poetry is a modern tool for Python dependency management. It simplifies package installation and project setup. This guide will show you how to use it.

What is Poetry?

Poetry is a Python packaging and dependency manager. It helps manage project dependencies easily. Unlike requirements.txt, it handles version conflicts better.

Poetry also creates a pyproject.toml file. This file replaces setup.py and requirements.txt. It keeps track of all dependencies.

Installing Poetry

First, you need to install Poetry. The recommended way is using the official installer.


curl -sSL https://install.python-poetry.org | python3 -

This command downloads and installs Poetry. After installation, verify it works.


poetry --version

You should see the version number. If not, check your PATH variable.

Creating a New Project

To start a new project with Poetry, run:


poetry new myproject

This creates a project structure. It includes pyproject.toml for dependencies.

Adding Dependencies

To add a package, use the add command. For example, to install requests:


poetry add requests

Poetry will install the package and update pyproject.toml. It also creates a poetry.lock file.

Installing from Different Sources

Poetry can install packages from various sources. Unlike .whl files or .tar.gz files, it handles them automatically.

To install from a local directory:


poetry add ./local/package

Managing Development Dependencies

Some packages are only needed for development. Add them with the --dev flag.


poetry add pytest --dev

This separates main and development dependencies.

Running Your Project

To run your script in Poetry's environment:


poetry run python myscript.py

This ensures all dependencies are available.

Updating Packages

To update all dependencies:


poetry update

To update a specific package:


poetry update requests

Removing Packages

To remove a package:


poetry remove requests

This updates both pyproject.toml and poetry.lock.

Conclusion

Poetry simplifies Python package management. It handles dependencies better than traditional methods. Start using it for your next project.

For other installation methods, check our guides on setup.py or local directory installation.