poetry basics

Poetry Basics: A Comprehensive Guide

March 26, 20242 min read

Poetry Basics: A Guide to Python Package Management

Introduction to Poetry

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and manages them for you. It's designed to simplify package management and deployment of Python projects.

Installing Poetry

To get started with Poetry, you need to install it. You can do this via curl or pip.

Using curl:

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

Using pip (for newer Python versions):

pip install poetry

Setting Up a New Project with Poetry

To start a new project using Poetry, you can either create a new directory or initialize Poetry in an existing directory.

To create a new project:

poetry new project-name
cd project-name

To initialize Poetry in the current directory:

poetry init

Managing Virtual Environments

Poetry can automatically create a virtual environment for your project to keep dependencies isolated. To configure this:

  1. To create the virtual environment inside your project's directory, use:

    poetry config virtualenvs.in-project true
  2. Specify the Python version for the virtual environment:

    poetry env use /path/to/preferred/python/version

Activating the Virtual Environment

To activate the virtual environment created by Poetry:

poetry shell

Managing Dependencies

Poetry makes it easy to add, remove, and list packages required for your project.

Installing Packages

To add a package to your project:

poetry add package-name

Removing Packages

To remove a package from your project:

poetry remove example-package

Listing Packages

To get a list of all the packages installed in the virtual environment:

poetry show

Conclusion

Poetry is a powerful tool for managing dependencies and streamlining the deployment process in Python projects. By handling package management and virtual environments, it allows developers to focus more on development and less on setup and configuration. With the above commands, you can easily manage your Python projects and ensure consistent environments across development and production setups.

blog author image

sunil s

Quant Developer & Mentor

Back to Blog