Modern Python Projects Transcripts
Chapter: Managing Python project
Lecture: Poetry in action
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's see how to use poetry to. Build a Python project. First, We need to install it. since poetry one of those
0:09
packages that you want to install globally on your computer, because you probably always want to use the latest version. I will use pipx to install it.
0:22
Once we have it installed, we can run poetry new in the name of our project. This will create a poetry demo folder with some files.
0:32
The most important one is the pyproject.toml. This is the main configuration file for our project when we use poetry. Now,
0:40
let's add some dependencies, to our project. We could modify the pyproject.toml, but that's not the point of.
0:47
Using a tool like poetry, we just install it, so that we don't have to manually edit files. So instead,
0:53
you can use the CLI command to add a new dependency. Let's go with the same example that we have been using so far and Let's add
1:01
pytest and Django. First pytest. We want to use the latest version, so we can run poetry at pytest. This created a virtual environment for us,
1:13
and then it tried to install pytest, but we have an error, based on the error message,
1:19
We can see that there was some problem with resolving our dependency because we are trying
1:23
to install pytest 6.1, and at the same time something is requiring pytest 5.2 So what can we do?
1:32
Well, let's take a look at pyproject.toml to see what's inside, as you can see when we create a new project with poetry,
1:41
Poetry adds a pytest dependency to this project, but its version 5.2. And we want the latest one, which is at the time of recording is 6.1.
1:53
So we can, for example, changed the version directly here. Now that we have modified the pyproject.toml directly
2:02
we have to run poetry install to install and resolve those dependencies. Oh, lets write this. All right, so this time everything worked fine.
2:15
And we have this nicely colored output saying which dependencies have been installed. Having conflicting dependencies can sometimes happen,
2:23
and the best way to solve this problem is to either pin or unpin some of the versions, depending on which version you actually need.
2:30
Now let's try to add Django. This time I want to use the latest version of Django 2.2, but not Django 3, just like we did in the examples so far.
2:43
I forgot to add the quotes, and this time we're more lucky. But we resolved all the dependencies for us.
2:53
When we run poetry installed for the first time, a poetry.lock file is created.
2:59
It's a slightly more complicated version of requirements.txt file that we were creating using pip-compile command in the previous lessons.
3:09
This poetry.lock contains pinned dependencies, so the next time you run poetry install, poetry will install those exact versions of Python packages.
3:19
So, don't forget to add poetry. Lock to your git repository. Behind the scenes poetry is using a virtual environment for
3:27
us, even though we don't really see it. As long as you use the appropriate poetry commands,
3:32
it will use that virtual environment, so we don't run pip install to install dependencies,
3:37
but we run poetry install. Now that we have installed Django, let's quickly create a simple Django website to see that it's working.
3:44
Don't worry if you don't know Django the next few commands that I will run will
3:48
create an empty Django project and start the Web server, So we can open a browser and see that we have successfully created an empty Django website.
3:57
As You can see I'm not running the Django admin command, but I'm running poetry run Django-admin. That way our command is run through poetry,
4:07
and it's using the virtual environment, plus all the dependencies that we installed with poetry.
4:19
We have a Web server running, so we can open it in the browser to see that everything is working fine. And there we have it.
4:27
Great, it's working. And if we do pip freeze, you can see that there are no dependencies installed globally. Okay, I have some packages,
4:35
but they are required by pipx. But as you can see, there is no Django here, and there is no pytest
4:39
Poetry is keeping everything in a virtual environment for us, and we dont even have to manually activate anything.