Poetry vs pip: Or How to Forget Forever "requirements.txt" Cheat Sheet for Beginners

Poetry is a dependency management tool in Python projects (analogous to the built-in pip).

It will be vital for beginners in Python to get acquainted with this tool, as it is a very simple and easy-to-use tool, the use of which can simplify the management and development of the project.

Installation

You can install poetry on windows either using pip:

pip install poetry

or with PowerShell (Windows)

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

pip stores dependency data in a file requirements.txt (or something else, but more often it is), poetry stores information in the pyproject.toml file, however, in the case of pip, only a list of dependencies and versions is stored in its file, and all basic information about the project is stored in .toml. It is very convenient, you can always find all the information in one place

To install dependencies in pip, you need to run:

pip install -r requirements.txt

poetry makes it easier and more beautiful

installation

poetry install

update

poetry update

Viewing dependencies in pip is done like this

pip freeze

You will only be able to see the current versions of the libraries and will not get the structure of all packages with their dependencies. In poetry, in poetry.the lock file, you can view information about all installed packages, the command:

poetry show --tree

It will show the tree structure of packages with their personal dependencies.

Also, launching a project in pip (in the case of a virtual environment) creates inconveniences, since the first thing you need to do is go into this very environment.

There is no need to activate the virtual environment in poetry, just go to the project folder and start using the commands. Poetry will find the right environment by itself.
You can also change the python version in poetry without having to change the old virtual environment.

This is only a small part of the benefits.
Now a little bit about the .toml file

[tool.poetry]
name = "new_proj"
version = "0.1.0"
description = "DEVoneLove"
authors = ["Vadim Kolobanov <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.10"
pygame = "^2.1.0"
icecream = "^2.1.1"
requests = "^2.26.0"
psycopg2 = { version = "^2.7", optional = true }
pymysql = { version = "1.0.2", optional = true }

[tool.poetry.dev-dependencies]
Pympler = "^0.9"

[tool.poetry.urls]
"My GitHub" = "https://github.com/vadimkolobanov"

[tool.poetry.scripts]
run-main = "new_proj.main:main_def"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
  • [tool.poetry] - contains basic information about the project

  • [tool.poetry.dependencies] - contains a description of all project dependencies. A link to Github is specified.

  • [tool.poetry.scripts] - contains scripts that need to be run when installing dependencies

  • [tool.poetry.extras] - dependency groups for a separate installation

  • [tool.poetry.urls] - Along with the main URLs, you can specify your own links

Conclusion

The study and effective use of new programming language features distinguishes a real programmer from a populist who talks about his skills more than he knows how.

(c) Vadim Kolobanov 2021 AD

Put on Heart if you liked it and you learned something new!

You can also follow ME to receive notifications about new interesting articles.

FAQ

I am a beginner, how should I learn Python?

Look into the following series:

Can we cooperate with you?

If you have interesting projects and you need a python (web)developer, then you can contact me by mail or discord and LinkedIn for cooperation

Connect to me on


To beat depression, try to just quit #programming πŸ€ͺ

β€” Vadim Kolobanov (@decodesperato)

13