Why do I recommend Poetry, and how do you get started?

Learn how Poetry is used instead of requirement.txt files; it will help you control your Python project more easily!

I have used requirement.txt from the first line of Python code I can remember; I have never looked back to choose some other way to install dependencies packages because I feel excellent and comfortable even when I sometimes really miss features for this old way to do it, in this case, I’m just one developer so its okay for now I think! :)

I see many benefits of using poetry today; one is that it is easier to use developer-only and production packages. Another handy benefit is how you run unittest, or integration tests, which become much easier when you have subfolders in your project, as I did.

So, let's create a basic poetry project and run some unittest code on it.

Install Poetry globally on your machine.

First, I recommend you use pyenv to run a multi-version of Python on your local machine; it is supported on Linux and macOS, so if you run on a Windows system, I can recommend you use a subsystem with, e.g., Ubuntu or another Linux distro.

When you have picked the right Python version, I use Python 3.12 right now, and then you should install pipx and Poetry on your global system.

Now that you have Poetry installed, you are ready to create your first Python project using Poetry.

Create Poetry project

So, let's create our first poetry project and see how it goes! :)

Now, if you list what's inside the new project folder, you will see a list of files and folders inside here.

You should focus on the pyproject.toml file for now. This file is what you used to use the requirements.txt file for, but a toml file keeps configurations for your project and is very useful.

However, when Poetry creates a new project by default, it can still be very confusing to use pytest and group your code. I do this by renaming my project folder to src and then creating a file structure like how I group my code, e.g., controller, model, and so on.

I need to explain my pytest configuration, including my src folder when it runs testing.

Update your pyproject.toml file.

Now we need to add pytest confiscation. Otherwise, it wants to understand if our application runs inside our src folder and should use src as the root path when it runs the pytest.

We will create our first test code by creating a test file inside our tests folder called test_my_func.py. You can paste my code into this file.

As you can see, we import my_func from the model, so we need to create this folder and file inside src/model/__init__.py

Now you are ready to run your first pytest with your Poetry code.

If everything is successful, you should have an output like this

Add dependencies package to Poetry project.

It's time to add the needed dependencies packages; I will give a sample of how you add and remove dependencies from your project.

When you add, it will add the package to the dependencies section, and when you remove it will be removed again very quickly! :)

Install your dependencies and get the lock file.

Now, it's time for you to start developing your new project using Poetry! :) So, let's prepare your project to start creating.

When you have installed your dependencies, you will see a new file called poetry.lock

You know this file if you have ever worked with Node and NPM. It keeps the packages in the right state when your project is installed. The lock file is essential when deploying your application or updating packages, so remember to commit it to your Git project.

Be social with me!

LinkedIn: https://www.linkedin.com/in/parisnakitakejser/
GitHub: https://github.com/parisnakitakejser
Instagram: https://www.instagram.com/parisnakitakejser/

Download the sample code from GitHub

You can download the sample code from the link below; it's a free sample I share with all my readers! :)

--

--

Paris Nakita Kejser
DevOps Engineer, Software Architect and Software Developering

DevOps Engineer, Software Architect, Software Developer, Data Scientist and identify me as a non-binary person.