0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In all projects in the Python profession, we suggest using the<strong>poetry</strong>tool. It can help you can manage dependencies. It is one of the most essential tools used by Python developers today.</p>
1
<p>In all projects in the Python profession, we suggest using the<strong>poetry</strong>tool. It can help you can manage dependencies. It is one of the most essential tools used by Python developers today.</p>
2
<p>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.</p>
2
<p>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.</p>
3
<p>In this lesson, we'll look at how to work with Poetry. We'll go through the following steps:</p>
3
<p>In this lesson, we'll look at how to work with Poetry. We'll go through the following steps:</p>
4
<ul><li>Installation</li>
4
<ul><li>Installation</li>
5
<li>Setting up</li>
5
<li>Setting up</li>
6
<li>Creating your first poetry project</li>
6
<li>Creating your first poetry project</li>
7
<li>Working with pyproject.toml</li>
7
<li>Working with pyproject.toml</li>
8
<li>Initializing a virtual environment</li>
8
<li>Initializing a virtual environment</li>
9
</ul><h2>How to install Poetry</h2>
9
</ul><h2>How to install Poetry</h2>
10
<p>Poetry creators wrote it in Python, but it's not the usual Python program you install using pip install. In the<a>introductory section of the Poetry documentation</a>you'll find commands to install the program on your operating system.</p>
10
<p>Poetry creators wrote it in Python, but it's not the usual Python program you install using pip install. In the<a>introductory section of the Poetry documentation</a>you'll find commands to install the program on your operating system.</p>
11
<p>When you install Poetry, it'll be available as a separate command in the shell:</p>
11
<p>When you install Poetry, it'll be available as a separate command in the shell:</p>
12
<p>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.</p>
12
<p>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.</p>
13
<h2>How to customize Poetry</h2>
13
<h2>How to customize Poetry</h2>
14
<p>If you ask for a list of Poetry settings immediately after installation, you'll see the following:</p>
14
<p>If you ask for a list of Poetry settings immediately after installation, you'll see the following:</p>
15
<p>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.</p>
15
<p>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.</p>
16
<p>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.</p>
16
<p>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.</p>
17
<p>Remember how you created environments with the python3 -m venv .venv command. In Poetry, you should do the same:</p>
17
<p>Remember how you created environments with the python3 -m venv .venv command. In Poetry, you should do the same:</p>
18
<p>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.</p>
18
<p>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.</p>
19
<p>Once we set up Poetry, it'll be ready for us to create our first project.</p>
19
<p>Once we set up Poetry, it'll be ready for us to create our first project.</p>
20
<h2>How to create your first project</h2>
20
<h2>How to create your first project</h2>
21
<p>Create a poetry project with the command poetry new NAME. Before running Poetry, check if the python3 command is available in the system.</p>
21
<p>Create a poetry project with the command poetry new NAME. Before running Poetry, check if the python3 command is available in the system.</p>
22
<p>Suppose you entered the poetry new first command first. It is what the result will look like:</p>
22
<p>Suppose you entered the poetry new first command first. It is what the result will look like:</p>
23
<p>The message Created package first in first package first in first means that a package called first was created in a directory called first with a corresponding __init__.py. Any Poetry project will always contain at least one package. In addition to the first package, the project already has a tests package. We don't need it yet, but it's worth knowing that real projects always have a test package.</p>
23
<p>The message Created package first in first package first in first means that a package called first was created in a directory called first with a corresponding __init__.py. Any Poetry project will always contain at least one package. In addition to the first package, the project already has a tests package. We don't need it yet, but it's worth knowing that real projects always have a test package.</p>
24
<p>You'll also have a README.md file in the actual project, which should contain the project description. This file has the .md - extension, which means it's a<a>Markdown</a>file. It is a popular markup format. By the way, we wrote this lesson in it.</p>
24
<p>You'll also have a README.md file in the actual project, which should contain the project description. This file has the .md - extension, which means it's a<a>Markdown</a>file. It is a popular markup format. By the way, we wrote this lesson in it.</p>
25
<p>The most important file in the poetry project is pyproject.toml. It is the file format<a>TOML</a>. You can read more about it here. The pyproject.toml file contains the project configuration. The information from this file is needed so that Poetry can:</p>
25
<p>The most important file in the poetry project is pyproject.toml. It is the file format<a>TOML</a>. You can read more about it here. The pyproject.toml file contains the project configuration. The information from this file is needed so that Poetry can:</p>
26
<ul><li>Manage project dependencies</li>
26
<ul><li>Manage project dependencies</li>
27
<li>Run code for execution</li>
27
<li>Run code for execution</li>
28
<li>Run development tools</li>
28
<li>Run development tools</li>
29
<li>Build a distribution and publish it on PyPI</li>
29
<li>Build a distribution and publish it on PyPI</li>
30
</ul><p>The more you work with Poetry, the better you are at publishing projects. You'll gradually get used to the fact that every package must have a description, a proper structure, and a specified version. We advise you to design projects according to these standards, even if you don't intend to publish the project in the index.</p>
30
</ul><p>The more you work with Poetry, the better you are at publishing projects. You'll gradually get used to the fact that every package must have a description, a proper structure, and a specified version. We advise you to design projects according to these standards, even if you don't intend to publish the project in the index.</p>
31
<p>We've created our first project. Now we need to deal with pyproject.toml.</p>
31
<p>We've created our first project. Now we need to deal with pyproject.toml.</p>
32
<h2>How to work with pyproject.toml</h2>
32
<h2>How to work with pyproject.toml</h2>
33
<p>The configuration file for a new project looks something like this:</p>
33
<p>The configuration file for a new project looks something like this:</p>
34
<p>Lines like [tool.poetry] describe sections with key = value pairs.</p>
34
<p>Lines like [tool.poetry] describe sections with key = value pairs.</p>
35
<p>We cannot place most keys in other sections. You can study the keys in more detail and find out where they belong in the<a>documentation</a>. Here we'll look at lines like these:</p>
35
<p>We cannot place most keys in other sections. You can study the keys in more detail and find out where they belong in the<a>documentation</a>. Here we'll look at lines like these:</p>
36
<ul><li>tool.poetry - describes the project from Poetry's perspective, where we store the version, description, and the name of the project</li>
36
<ul><li>tool.poetry - describes the project from Poetry's perspective, where we store the version, description, and the name of the project</li>
37
<li>tool.poetry.dependencies - stores a list of dependencies required for the code to work. Python itself will always be listed here. We can't run the code without it</li>
37
<li>tool.poetry.dependencies - stores a list of dependencies required for the code to work. Python itself will always be listed here. We can't run the code without it</li>
38
</ul><p>The build-system section indicates that we have a project managed by Poetry. That said, there are other tools to manage projects. The build-system section looks different for them. All that's left is to deal with the last step - the initialization of the virtual environment.</p>
38
</ul><p>The build-system section indicates that we have a project managed by Poetry. That said, there are other tools to manage projects. The build-system section looks different for them. All that's left is to deal with the last step - the initialization of the virtual environment.</p>
39
<h2>How to initialize a virtual environment</h2>
39
<h2>How to initialize a virtual environment</h2>
40
<p>Poetry realizes that it's time to create or update the environment. In this case, you can call poetry install:</p>
40
<p>Poetry realizes that it's time to create or update the environment. In this case, you can call poetry install:</p>
41
<p>This command installs all dependencies in the environment in the .venv directory inside the project.</p>
41
<p>This command installs all dependencies in the environment in the .venv directory inside the project.</p>
42
<p>The environment is activated with the command poetry shell and terminated with exit:</p>
42
<p>The environment is activated with the command poetry shell and terminated with exit:</p>
43
<p>It's often not necessary to activate the environment. Poetry offers a run command which executes programs from within the environment. For example, it's customary within the project to launch the REPL with the poetry run python command.</p>
43
<p>It's often not necessary to activate the environment. Poetry offers a run command which executes programs from within the environment. For example, it's customary within the project to launch the REPL with the poetry run python command.</p>
44
<p>It is what working with Poetry looks like. It is a handy tool, so we recommend using it. Make your experiments look like Poetry projects. It will help you learn how to use Poetry faster.</p>
44
<p>It is what working with Poetry looks like. It is a handy tool, so we recommend using it. Make your experiments look like Poetry projects. It will help you learn how to use Poetry faster.</p>
45
<h2>Conclusions</h2>
45
<h2>Conclusions</h2>
46
<ul><li>Poetry is a handy tool that simplifies project development and maintenance</li>
46
<ul><li>Poetry is a handy tool that simplifies project development and maintenance</li>
47
<li>Poetry isn't installed via pip install, we use special commands found in the Poetry documentation to install it</li>
47
<li>Poetry isn't installed via pip install, we use special commands found in the Poetry documentation to install it</li>
48
<li>Every project needs to have a virtual environment. Poetry needs to be pre-configured for this</li>
48
<li>Every project needs to have a virtual environment. Poetry needs to be pre-configured for this</li>
49
<li>Each package must have a description and the correct structure and version assigned</li>
49
<li>Each package must have a description and the correct structure and version assigned</li>
50
</ul>
50
</ul>