DEV Community

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

Posted on • Updated on

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

You can find many interesting articles on my website, please visit it Happy Python Website

Automatic translation in the browser will help you to study everything conveniently.

If the material is useful, I will try to translate all my articles for you

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
Enter fullscreen mode Exit fullscreen mode

or with PowerShell (Windows)

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

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
Enter fullscreen mode Exit fullscreen mode

poetry makes it easier and more beautiful

installation

poetry install
Enter fullscreen mode Exit fullscreen mode

update

poetry update
Enter fullscreen mode Exit fullscreen mode

Viewing dependencies in pip is done like this

pip freeze
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 <titanyforgame@gmail.com>"]

[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"
Enter fullscreen mode Exit fullscreen mode
  • [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:

Learning Python
Step by Step to Junior
Ideas

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

Write me on Facebook

My Twitter


To beat depression, try to just quit #programming 🤪

— Vadim Kolobanov (@decodesperato) <a

Top comments (2)

Collapse
 
turry profile image
Turry

Amazing guide to poetry but I’d recommend not to install using pip and instead use the official way on their website (quick google poetry python and the first link is it).

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

A very useful remark, I hesitated for a long time whether it was worth complicating it)