DEV Community

Cover image for Python Poetry Dependency Management in 2 Mins
Pratik Pathak
Pratik Pathak

Posted on

1

Python Poetry Dependency Management in 2 Mins

Hi Everyone !!!
We all face issues with the requirements.txt file when we set up a project. But somehow it always runs into some issues which we don't know.

Reasons not to use Requirements.txt for dependency tracking

  1. It requires lots of manual effort to manage dependencies
  2. Needed different requirements.txt for different environments. Example Dev: requirements-dev.txt and for Production: requirements-prod.txt
  3. Unavailable Packages. Sometimes the package we mention expires which causes issues in the future.
  4. No way to specify Python requirements in packages.
  5. No way to tackle the Diamond Dependency issue

Don't know what is Diamond Dependency?
click here to find out: Poetry Python Full Tutorial

Poetry the Savior

To Solve this issue, Poetry became easier, Poetry easily maintains all the dependencies for you. You no longer need to do anything manual work. It also creates a Virtual Environment for you.

How to install Poetry?

pip install poetry
Enter fullscreen mode Exit fullscreen mode

Poetry All Commands one by one

Basic Commands 💻:

poetry init : Initialize a new project
poetry add package-name: To add dependencies
poetry remove package-name : To remove dependencies
poetry update : To update dependencies
poetry install : Install dependencies from pyproject.toml

Virtual Environment 🦋:

poetry shell : Activate the virtual environment
poetry env use : To Specify Python Version
poetry env list : List all the environment
poetry env info : View environment information

Dependency Management 🏬:

poetry show : List installed dependencies
poetry lock : Generate Poetry lock file

Wanna learn Poetry in deep with examples?
Full Tutorial Available on Python Poetry Tutorial with Example

Custom Commands and Scripts 🗃️:

Define entry points in “pyproject.toml”

[tool.poetry]
scripts = { test-all = "pytest tests/unit -v --cov=./coverage ./integration_tests.py" }
Enter fullscreen mode Exit fullscreen mode

Run custom commands

poetry run <command-name>
Enter fullscreen mode Exit fullscreen mode

Building and Publishing 📰:

poetry build : Build the package
poetry publish : Publish the package to a repository

Full Cheatsheet available on my LinkedIn. Get it from my LinkedIn: Pratik Pathak
Github: zpratikpathak
Website: PratikPathak.com

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay