In all projects in the Python profession, we suggest using the poetry tool. It can help you can manage dependencies. It is one of the most essential tools used by Python developers today.
Poetry is a simple and handy tool that makes it easy to maintain and develop projects. We advise you to design each learning experiment as a poetry project so that you get used to professional tools faster.
In this lesson, we'll look at how to work with Poetry. We'll go through the following steps:
- Installation
- Setting up
- Creating your first poetry project
- Working with
pyproject.toml
- Initializing a virtual environment
How to install Poetry
Poetry creators wrote it in Python, but it's not the usual Python program you install using pip install
. In the introductory section of the Poetry documentation you'll find commands to install the program on your operating system.
When you install Poetry, it'll be available as a separate command in the shell:
poetry --version
Poetry version …
That's all about installing the program. Before we create our first project, let's set up the tool to make it more convenient for us to work.
How to customize Poetry
If you ask for a list of Poetry settings immediately after installation, you'll see the following:
poetry config --list
cache-dir = "/…/…/.cache/pypoetry"
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.path = "{cache-dir}/virtualenvs" # /home/astynax/.cache/pypoetry/virtualenvs
Poetry works with virtual environments. And it's initially set up in a way that means you'll have many different versions of Python. For this reason, the tool creates virtual environments for projects in an unusual location - note the virtualenvs.path
setting.
You can use the default settings, but Python developers usually store the virtual environment for each project in the project directory, specifically in the .venv
subdirectory.
Remember how you created environments with the python3 -m venv .venv
command. In Poetry, you should do the same:
poetry config virtualenvs.in-project true
Now every poetry project will have a virtual environment. For example, you can transfer a project from one machine to another by copying the directory.
Once we set up Poetry, it'll be ready for us to create our first project.
How to create your first project
Create a poetry project with the command poetry new NAME
. Before running Poetry, check if the python3
command is available in the system.
Suppose you entered the poetry new first
command first. It is what the result will look like: