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