How to use Poetry to manage Python projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign ER here in this episode of Dev with sirdar  and we're going to take a look at the Poetry   dependency management and packaging system  for python poetry is a way to handle working   with dependencies and virtual environments for  multiple projects and for multiple editions of   python so if you're creating something you want  to eventually deploy to Pi Pi or allow other   people to install and reuse poetry automates a  lot of the setup and packaging steps that are   involved it also ensures the requirements you  need can be reinstalled reproducibly and again   it will work across multiple editions  of python installed on the same system   so let's take a look at the way poetry works  with a simple example now the first thing you   learn about poetry is that it doesn't install  in any one given instance of python it installs   system-wide as a separate application and it  works with whatever python installations you   already have in your system so this way you  can use it across multiple python versions   and poetry can also update itself apart from your  python installations or virtual environments so   it's not locked to any of them now when we  set up poetry in a system we can do this one   of two ways we can use a system-wide installer  or we can use pip and the first way which I'll   show here involves a command line operation and  honestly it's the recommended way to do it you   can use pip to do it but it's not as reliable  because of the way poetry is supposed to work   so this command that I'm Hasting by the way is  described in poetry setup documentation and what   it does is it fetches the needed installation  bits from poetry site and gets it running   note that you should run this in an admin level  command line not from a user level command line   so once we have poetry installed we then have the  Poetry command available at the command line as   a utility and this makes working with python a  little bit more like the tool chains you see in   modern languages like rust and go where you have a  single common command that handles a lot of basic   project management functions a side note by the  way one of the things the installer process will   not do is add the path to the Poetry executable  to your system path that is a manual step   and I've done this ahead of time as you  can see here from the path statement and   this is again something described in the  docs for the setup so follow those closely   so to start an entirely new project with poetry  we use poetry new as our Command followed by the   name of the project we want to create and this  creates a directory with that name and populates   it with some basic scaffolding for a project this  includes a pi project.toml file Pi project.toml is   the modern way for python projects to describe  things like project metadata or dependencies   and we also get subdirectories with layouts for  a test Suite we get a readme file and of course   we get the project code itself now if you've  already got a project directory you can convert   it to use poetry by using a different command  poetry init for the sake of Simplicity I'm just   going to work with a newly created project but I  am going to paste in some code that I've pre-made   here for the sake of using this to make things  move a little faster now we're going to add some   third-party dependencies that we need for this  project to do that we use another command poetry   add and we Supply the dependencies we want to add  so in this case I'm using the piglet library for   windowing so we're going to add that now when  we perform this step by the way this particular   step creates if we haven't won already a virtual  environment for this project and what's important   is that this Vim is by default held in a centrally  managed cache separate from any project directory   now if we want to place that end in the project  directory itself we can do that if we want to and   we can switch to different vims within a project  if needed but I want to demonstrate the default   Behavior to give you an idea of what that's like  I should also note that by default the package   source for any dependencies you install is Pi Pi  the package repository used by default for pip   but you can install from other locations if you  need to such as a git repository or a local path   now once the dependencies Finish adding you'll  see that we now have a poetry.lock file in our   project and this tracks all of our dependencies  all their versions their interrelationships   it guarantees we can get a reproducible  deterministic build of everything we use   so this is another feature of poetry  is that it works out and guarantees   for us that the dependencies we have will be  consistent if we give this to somebody else   oh we also now have since this is Visual Studio  code a poetry managed virtual environment detected   for us and we can switch to that for the  sake of using our linting tools in the editor   now let's say I want to add another dependency  like the black code formatter but I don't want   to use it in the core project it's just for  the sake of our development and this is where   poetry dependency groups come in handy so we  can say poetry add dash dash group Dev black   and what this does is it installs the black  dependency but it puts it in a separate set of   dependencies labeled as Dev so if I'm not doing  development with this project I don't have to   install that dependency set it's kept separate  so we can track dependencies based on their use   now to run this project I can use the Poetry  command tool poetry run pie and then the program   name and that will bring up a simple window  this is the Bare Bones of what will eventually   be a bigger program but we now have our editor  configured to use the correct Vim if created   by poetry so we can just use the editor's  built-in run Command 2 if we want to do that   now if we want to work with the vent that's been  created for us at the command line poetry also   provides us with another convenience for that  poetry shell so if I'm using my project as the   current working directory I can type that and  bring up a prompt that has this vanvactivated   I should note at this point that the venv is  already Auto activating in the command line   for my editor when I open it because I've  already set it up for use with this project   but it is useful to be able to do this manually  from a command line that spawns somewhere else   one other thing poetry makes very convenient is  publishing your finished package to Pi Pi that's   the common repository for python packages that's  a detailed enough subject that it would Merit   its own discussion but the short version is that  once you've got your Project's name spacing and   internals configured you can just upload it to Pi  Pi by just typing poetry publish finally because   poetry manages its Vans centrally by default you  do have to use a little bit of extra care when you   remove a ven for project if you're getting rid of  the project entirely for instance or if you want   to wipe the vent and start over so to do this  you would use poetry and remove python in your   project working directory and that would remove it  for you so there is an advantage that comes with   keeping a van with its project directory which is  that you can erase it manually just by removing   the directory but as I mentioned before you can  always relocate the vent if you want to do that   so to sum up poetry provides a way to manage  virtual environments and dependencies for multiple   python projects and multiple python versions  separate of any one project or version of python   it gives you a consistent command line interface  for creating projects for adding and managing   dependencies for resolving conflicts within  them and for working with virtual environments   so that's it for this quick tour  of poetry in this episode if you   liked it leave a comment below and  don't forget to follow Dev with   sertar and infoworld on Facebook  YouTube and infoworld.com [Music]
Info
Channel: InfoWorld
Views: 7,535
Rating: undefined out of 5
Keywords: How to code, Coding tutorial, App dev, App developer, Python, Python poetry, Python projects, Python virtual environments
Id: KVgG5QRypZI
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Tue Apr 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.