In this lesson, we'll take a closer look at working with dependencies with Poetry.
Adding and removing dependencies
In the last lesson, we created a project named hello
. Let's return to it and add the colorama
package — a dependency for hello
. It is a popular library that allows you to colorize text in the terminal.
We will add dependencies with the command poetry add NAME
:
poetry add colorama
Using version ^0.4.5 for colorama
Updating dependencies
Resolving dependencies... (0.8s)
Writing lock file
Package operations: 1 install, 0 updates, 0 removals
• Installing colorama (0.4.5)
Now we will look at the tool.poetry.dependencies
section of the pyproject.toml file. You'll find the following:
[tool.poetry.dependencies]
python = "^3.10"
colorama = "^0.4.5"
As you can see, colorama
is installed in the virtual environment and appears in the list of project dependencies. If we want to run a code, we can run the poetry install
command and get all the necessary dependencies.
Note the text "^0.4.5"
. It doesn't just mean specifically 0.4.5. The versions from 0.4.5 up to 0.5.0 will be considered compatible. You can also give a specific version, list several permissible versions, or manually specify a range. You can learn more about this variety in the documentation.
Now let's try to remove the dependencies with the command poetry remove NAME
. Python will remove the uninstalled packages from the pyproject.toml file automatically:
poetry remove colorama
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
Package operations: 0 installs, 0 updates, 1 removal
• Removing colorama (0.4.5)
Dependency groups
Many tools for developing Python projects are in Python. However, they're usually not needed to run the code. Take, for example, Pytest, a popular framework for writing tests. This package has many dependencies of its own. In a large project, these development tools can take up a lot of space.
The problem is that users do not need all these dependencies because they do not run any tests. However, the user gets all the dependencies with the program and wastes space by having to store them.