Register to get access to free programming courses with interactive exercises

Getting started with Poetry Python: Setting up the environment

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 = 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 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:

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.