DEV Community

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

Posted on

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

Top comments (0)