hi there sardar yagelaov here for infoworld at idg in this episode of smartpython we're going
to explore a relatively new way to perform package management in python using a project called pdm
short for python development master a pdm is a package management tool for your projects that
makes use of a recent addition to python standards known as pep582 which was introduced in python 3.8
it manages and installs packages for a project resolves conflicts with versioning and most
importantly it does not use a virtual environment this cuts down significantly on the amount of
space needed for your project directory now you can use virtual environments with pdm if you need
to but for some people pep 582 based solutions are going to be much cleaner and more manageable
and that's what pdm is going to offer you so in this video i'm going to walk through setting up
and using pdm for a very simple web project for this i'm going to use the bottle web framework in
conjunction with the jinja 2 templating system bottle itself has no dependencies which is one
of the reasons i like it but jinja 2 uses at least one dependency and there will be other
projects i'll be installing along the way that will include other dependencies as well
the first thing we have to do is set a pdm itself and this we can do by installing it into our
underlying python installation for the sake of convenience we can use pip to do that by simply
typing pip install pdm for the sake of convenience i've already installed pdm on my current system
and as you can see it will install a fair number of packages so if you're hesitant
about using it in your base installation you can always try it out in a virtual environment first
just remember to activate the virtual environment before using it in your projects
the next thing we need to do is ensure python uses pep582 properly and this we can do by way
of setting the python path environment variable i've already done this in my system again for
the sake of convenience but if you want to do it automatically on your system you can run
pdm-pep582 to either generate the commands needed in mac and linux or to execute them automatically
in windows now note that once you do this you're going to need to restart your shell sessions to
have it take effect if you execute this from the shell sessions inside an editor such as in
visual studio code here you're going to need to restart visual studio code completely
my suggestion is that once you do this you should just reboot as it'll take care of many things at
once the pdm command line tool by the way is the front end for all of pdm's functionality
it's what we use to configure and manage python projects with pdm
so let's start by configuring our project with it if we type pdm init this will take the current
directory that we're in and we'll install into it the metadata files used by pdm to manage a project
now when this happens we'll be prompted to answer some basic questions like which edition of python
to use but you can generally just press enter to use the defaults for each of these questions
the only one you might need to think about is whether this is going to be a
project that's going to be uploaded to pipi most of the time this won't be next step
is to add dependencies for the project and this we can do by just typing pdm add and then providing
one or more dependencies pdm will figure out any conflicts that might arise from the dependencies
we ask for and it will track those dependencies throughout the lifetime of the project
if we type pdm list we can pull up a list of all of the install dependencies
and if we use pdm list dash graph we can see the dependencies visually here we can see how jinja 2
has markup safe as one of its dependencies now at this point i should talk a little bit about pet582
and how pdm uses it pep582 specifies that if you have a directory named pi packages in the
current working directory of a python application like this its contents get automatically added to
the package path for that application now when pdm installed our dependencies for this project
it created a pi packages directory a subdirectory for the version of python we're using
and subdirectories in there to hold the dependencies and scripts that we need
this way any packages that we add to this project will get placed there as part of the
project and will be automatically included in the package search path when we run our program
we don't need to create a virtual environment and we don't need to activate it
so this saves us both disk space and hassle because virtual environments can get kind of big
right now my project only consists of a single file main dot pi and to run it we don't have to do
anything special we just run it from the command line as we normally would any python program
the packages that we need for it are already in pi packages and they will be discovered automatically
by the python interpreter now when in the future we want to update any of our dependencies
we can do this with the pdm update command we can update one package
like this with bottle or we can attempt to update everything by just typing pdm update
and if we want to remove packages we can use pdm remove which will clear out both
the given package and any dependencies that can be safely removed along with it
if we remove jinja 2 for instance markup safe is also removed since only jinja 2 requires it
another thing that we can do and this is highly useful in the development process
is add dependency groups we can have one set of dependencies that are used
for just running the program another set of dependencies for the development process
another set for testing and so on and so forth so let's say we want to add the black formatter
to the project but we only want to add that as a dependency for the sake of development not running
so to do this we would use the dg flag with pdm add like so pdm add dash dg dev that's the name
of the dependency group that we're using and black once black finishes installing and all
the dependencies also finished computing you can see now we have a different dependency graph for
the project by the way because pep582 is still fairly new and not widely used not many editors
know how to work with it with visual studio code for instance we need to manually specify in our
project's settings.json path information for how the editor should perform auto-completion
and code analysis it isn't hard to do this pdm's own documentation includes information about how
to do it and i have an example right here but it is something that you need to include manually
if you want visual studio code to work correctly with code linting tools when you use a pdm managed
project now all this only scratches the surface of what's possible with pdm but it should be enough
to give you a good idea and especially how using pi packages can make package management a little
more flexible with tools that understand it pdm is one of the first of those tools
and i encourage you to at least try it out in a new project to see whether or not you like it
that's it for this video if you like it please leave a comment below don't forget
to subscribe to the idg techtalk channel on youtube and for more smart python and
more dev with serdar be sure to follow me on facebook youtube and infoworld.com