Setup a Python project with PDM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm going to be honest with you this video was supposed to be about poetry but while working on a custom console application I had a real trouble with installing open AI whisper through it so I've scoured the internet for other tools I went on redit and other places like that and I found a lot of recommendations for a tool called PDM so I've tried it and it just magically worked like just like that then I've dug deeper and looked through a documentation I was pretty impressed with the tool and I'm definitely going to use it in the future but even if you're less willing to experiment like me and want to use poetry I think this video will still be plenty helpful we'll have a look into creating a new project adding dependencies and in the end I'll show you how to easily install your custom application all right let's go the reason why we're talking about those tools is Imports yeah pretty much that's it and there are three kinds of inputs in Python first we have the standard Library those are all the packages that we import from python so the only thing that really matters for those to work is which version of python are you running then we have external dependencies those come from the internet usually from the python package index if you haven't used tools like PDM or poetry before you might have installed those through pip finally we have our internal Imports those come from the fact that we don't want to keep all of our codes in a single file so at a certain point we will split our project into multiple so-called modules and packages and using a tool like PDM will make all of this so much easier let's install PDM we need to go to its documentation then ignore the scary looking curl and go straight to pip X pip X does exactly the same things that that the curl would do which is manage pdm's installation outside of the global python environment but it's a reusable tool that you can use for installing many other things like poetry cookie cutter anible pre-commit or even open AI whisper and our own custom applications as well all right now time to install pipx again we go to its documentation and we see that for Mac OS the recommended way to install it with brew and for other systems with Pip after it's installed we need to run pip X ensure path and restart our shell now we're ready to install PDM with pipx install PDM creating a new project could be a little bit better if you've used poetry you might be a little bit disappointed but it's not the worst thing ever first you need to create a folder and as always make sure to use Kebab cakes that is lowercase letters numbers and dashes now you have to go into that folder and run PDM in it PDM in it is one of those tiny CLI applications that you might be familiar with if you've created any projects in the JavaScript space first we'll have to pick our interpreter if you have multiple just make sure it's the correct version here we'll choose python 3.12 then say yes to create a virtual environment that's a default so you can just press enter now there's an important thing here make sure you answer yes to whether you want an installable Library it's not a default but I really don't like the default version as the setup will not include the name of our package and it will make it a little bit annoying to work with the things that are added on top of our installable project are not that annoying and there there's very small amount of them anyway and we'll have a look into exactly what those goodies are but make sure to set it to yes for the rest of the setup you'll want to go with the defaults name is just the folder name and version description and backend don't really matter that much for our case for license pick the default MIT and you can remove it later if you want to author name and author email should be defaults and those will be based on your git configuration and for python version also pick up the default time to open up our project in vs code it seems that PDM has created a bunch of files and folders for us the most important one is p project tomel this is where most of our project configuration is we'll have a look into it a little bit later then there's readme.md which is a nice file to edit if you want your GitHub repository page to look pretty we have G ignore and a bunch of files and folders that will be ignored by ITV contains our python virtual environment this is something like a very lightweight python installation it contains an interpreter and a bunch of dependencies but it also reuses some of the stuff from the global installation it's our way of isolating The Interpreter and dependencies between projects you can also see pie cach folder this one contains a bunch of cache and you might see it all around your project you should ignore them with Git and probably also ignore them in general the last file that will be ignored by git is PDM python this file informs PDM which python interpreter it should use again something that will be managed automatically but you might want to have a look into it whenever you for example need to tell vs code which interpreter to use it should figure it out by itself but sometimes it's necessary then there's the SRC folder this is is where our codes are yes finally inside of it there's a folder called PDM tutorial not is how instead of a dash we have an underscore here that's because this folder is also a python package and by extension a python module so it has to be importable within python we know that it's a python package because it has a dander init.py file in it it informs python that this folder is indeed a python package right now this package is empty but in a second we'll fill it out with code if you've used poetry before you might have noticed that poetry will usually not have the SRC folder but instead it would put the main package within the root directory you can tell poetry to create an SRC folder by using a-- SRC option no matter whether you're using poetry or PDM I highly recommend having the SRC folder lastly we have the test folder you might have noticed that it is also a python package that's because our test Runner will be importing test modules from it time to create our first code we'll start with dander main.py this file will change our package into a runnable module it's recommended not to use Dunder name equals Dunder main pattern in it and instead make it so small that there's basically no way to import anything from it while in most cases Dunder name equals Dunder main with Imports will work well with this file in some specific situations it's not possible to import from this file at all so it's better to use it as a tiny entry point to our package we can also create a regular script without using any danders at all using dander name equals dander main is recommended while you can run regular Python scripts with just the Python 3 command and in Virtual environments just python we are going to use modules so things will be a little bit more complicated and by a little bit more complicated I mean we'll have to use the dasm option so python DM and then the name of our module but first we need to install our package this involves us running PDM install from within our project you can notice that this install is an editable install which means that our project files will not be copied into the virtual environment but just linked to it you can see it here to run our dander main.py file all we need to do is use PDM run python minus M PDM tutorial command it looks a little bit too complicated but the PDM Run part is actually not necessary if you're running from a virtual environment which in vs code should be the case by default so python DM PDM tutorial is not that complicated if you want to run any other module in your project just just use the dot notation by the way there's a reason why we use all those modules we can split our code and import from one to the other using the dot notation as well and you can see that because of our Dunder name equals Dunder main pattern the imported part runs only once let's add our first dependency we'll choose one of the most popular python packages which is requests it's a very simple HTTP library that will use mostly as a dummy in this example all we need to do is run PDM add requests and we can see in P project toml that requests were indeed added to our dependencies you might have also noticed a scary looking file called PDM do loock don't worry about it it's automatically generated and it's there to remember specific versions of your dependencies so that everyone who works on your package or you on multiple machines have the same versions of the depend depencies which makes everything much more stable you can see that the dependency was added with the greater equal sign which is not the default in poetry poetry uses carrot and till dependencies and those while great are not part of the Python standard so they're unavailable you can instead use Python's compatible dependencies but keep in mind those are not the same as poetry still dependencies or you can manually set up dependencies that work like current dependencies but it would be really nice to have this automated as one of the configs well it is what it is all right time to create a simple code that uses requests now let's run it and yeah it just works finally let's have a look at Piper Tom we've already looked at the part with dependencies so let's go back to the top of the file we we have our project name and then there are a few things that shouldn't really matter to you unless you want to publish your library there's version description authors and read me and those are basically only for package indexes oh and there's also license as I've mentioned before feel free to remove it or change it then there's the build system which only matters if you want to build your package the default should be fine anyway all right all right it's finally time to make our application installable what we need is a function probably named Main and we don't want to import it from Thunder main. Pi file because as we've talked before we shouldn't really import from that file and what we're doing right now will involve hidden Imports if you want your entry point to look neat and simple you can Define or reimport your main function in under init.py and now time to specify our entry point in P project toml and we're ready to go all we need to do is run pipx install Dot and now we have our globally installed python application and that is it so let me know if there's anything in particular that you would want to learn about more if you like PDM make sure to go to its GitHub page and give it a star and if you've enjoyed the video like And subscribe
Info
Channel: Orchard Dweller
Views: 4,316
Rating: undefined out of 5
Keywords: python, pdm, poetry, tutorial, guide, coding, learning, programming
Id: cOFyf0_CDhI
Channel Id: undefined
Length: 12min 22sec (742 seconds)
Published: Thu Nov 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.