Transcript for:
Introduction to Poetry for Dependency Management in Python

as soon as you start writing more complicated Python scripts it becomes really important to properly manage your dependencies you're going to use a bunch of other libraries and packages and you want to make sure that you have the versions right commonly used tool to handle dependencies is poetry which is what I use for most of my python projects and next on handling dependencies this can also do other things such as creating virtual environments for your code to run in but also publish your code your packages to piie so today I'll walk you through the basics of poetry give you some tips and tricks for how to set up and manage your project effectively before we start I have something for you it's a free guide that teaches you how to design a new piece of software from scratch you can get it at iron. c/d design guides it contains the seven steps that I take whenever I design a new piece of software and hopefully it helps you avoid some of the mistakes that I made in the past rn. c/d design guides link is also in the description of this video dependencies in Python have been handled in different ways in the past one very basic way to handle dependencies is just pip install whatever you need in your Global python installation that's not a really great way to go because that means that you may have version conflicts it will also be hard to keep track of when you want your script to run somewhere else what dependenc you actually need to install so it makes sense to at least have some representation of what the dependencies are supposed to be as part of your python project common way to do that has been requirements do txt but it's easy to misconfigure and kind of hard to update and that's why since pep 518 the P project. Tomo file has been introduced here you see an example of what such a file might look like so it has various sections so there are General package settings like the name and the version description you can refer to read me file there are dependencies in this case we have a python 3.12 dependencies and you also have built system settings by putting all of this information into a single file you don't need a separate requirement. dxt and a setup. pii a manifest and other things it's all in one tal file now you can in principle directly edit this file it's a text file so you can just add your dependencies here but it's kind of cumbersome and uh it's in my opinion better to use a tool like poetry to handle that automatically for you for example if I want to use poetry to add a dependency on py test I can simply write poetry at py test and now you see it has installed a bunch of different things and if we open the Tomo file poetry now has added py test for us and on top of that it has also installed other packages that py test depends on and if you type poetry show then you can see the packages that it has installed including the version so this you can run at any time to see what is the status of your dependencies the first time you add a package poy also creates a po. loock file and this is what that looks like now this is a file that you typically don't have to edit yourself it's really an in internal file for poetry to keep track of the dependencies next to just adding a dependency poetry has other options as well you can also remove dependencies or update them to the latest version using semantic versioning let's explore this a little bit more poy add is used to add dependencies like I've just shown before so for example let's say I want to add the requests package which is quite commonly used for HTP requests I can use the AD Sign to specify a specific version for example 2.2.1 so when I do this now it has added request version 2.2 and you can also see it's here in the list and when I open the by project file you also see that the request package has been added here and as you can see it has installed this specific version so if I want to remove the dependency I simply write both remove request in this case and now it has removed that particular dependency now typically I don't install specific versions of packages I just want to give some sort of limitation into how we want the upgrade process to take place so what I typically do is install the package like so and what this character does that it installs the most recent version following this version up until the major version so it won't install request version 3 for example but as you can see it goes up to 2.31 Z so it's a minor version and not a major version update and when you look at a p project file you also see that has added this specification to the version number now if I type poetry show and I can even add the specific package that I want to show then you see it shows the name of the package the description as well as the version that's actually installed or just WR poetry show to show everything and there you also see there's a request package at 2. 31.0 let's remove that dependency again so another alternative is that we add requests using the tier and the til is a bit more restrictive in that it only installs the package up to the latest minor version so you see here it has installed 2.2.5 instead of 2.12.1 but it doesn't switch to 2.13 so basically what you do in both of these cases that you sort of Define an upper and a lower bound and when you're just starting out with a new project you probably just want to add the latest version of a package but it's helpful to know that this gives you some control over what dependency version you're going to use what I'd recommend you use when you install dependencies is the carrot that to me is a good sweet spot between having maintenance overhead and having no control whatsoever over the versions and by the way this is also what po2 does by default so for example if I add fast API then you also see if I look at by project Tomo file that it actually uses the carrot character by default when you have a pip project file you simply want to install all the dependencies in it you write poetry install and then it's going to Simply install all of the dependencies so you see there is in this current project no specific folder for this particular package so you can also say that there is no root folder and then you don't get that aror but it does install dependencies in both cases now when you have the dependencies installed po can also create a virtual environment for you within which you can run your code and you simply write poto shell to go into that virtual environment and then within that environment you can simply run your python script so it's really easy to use in the vs code teral you can actually see which virtual environment you are in and in this case in my recording folder it has created a VM folder that contains all the information related to the virtual environment now poetry also offer some help with versioning building and Publishing your package So currently the version of this package is 0.2.0 but if we want to bump the version number we can simply write poetry version minor and this uses semantic versioning to go to the next version so this goes from 0.2.0 to 0.3.0 and this also reflected in the PIP project file normally the PIP project file is something that you commit in your git repository so that the latest version number is also available there now there are other things you can do related to semantic versioning like 1.0.0 Das Alpha One or release candidate there's lots of things to cover I might do a separate video about semantic versioning if you like me to do that let me know in the comments now when you're ready to unleash your project into the world po can also package your project into distributable format such as a wheel or a source distribution tar Gip file and then when you don't building you write poetry publish and then Supply your pipy credentials then you can publish it to pipy and of course all of these things you can also do that in let's say a GitHub workflow so that whenever you commit to a particular brand that's automatically going to run these task or whenever you publish a tag on GitHub lots of possibilities there to automate things and simplify your life so I hope this gave you a quick idea of what you can do with poetry highly recommend you start using it now if you want some inspiration some ideas for nice python projects you could work on to improve your skills watch this video next thanks for watching and see you soon