Makefiles in Python For Professional Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in today's video we're going to learn how to use make files for python in order to automate processes like building projects installing requirements and running scripts so let us get right into it [Music] all right so we're going to talk about make files in the python context today now make files are not something that belongs to the python ecosystem it's something that is language independent so make files can be used in a c context in a c plus context Java context and so on with different libraries different Frameworks different languages and the basic idea is that we Define certain procedures like building a project installing requirements running the project setting up something or cleaning the project and stuff like that we have these certain procedures defined with names and with sets of instructions and we have all those defined in a make file so that we don't always have to run the same commands we can just say make install make run make clean make build and so on to execute these procedures and today we're going to look at what this looks like in Python and for that we're going to use a very simple structure we're going to have a directory which we're going to call now my package and inside of that directory we're going to define a main.py file here we're going to now say print hello world this is going to be our uh functionality and then we're going to also Define a file called underscore underscore init underscore underscore py this is basically just defining this as a package so when we have a knit file in here this is recognized as a package and then we also want to have outside of this directory another file which we're going to call setup.py and for this we're going to need a library called or a package called setup tools so we're going to open up the command line and we're going to type pip install setup tools and this is going to be our build file so once you have set up tools installed you can go into that file and we can say from set up tools import setup let me just uh zoom out a little bit set up and find packages um and then what we're going to do is we're going to say setup this is the function and we're going to define a couple of things here first of all the name of the project is going to be I don't know my project name then we're going to say that the version is going to be 1.0 and then we're going to say that the packages that we want to include in this uh in this final package is going to be the result of find packages so we're going to call find packages this is going to recognize the my package since it has an init file in it and then it's going to build it when we call it properly that is the basic structure of what we want to do here now in this context we can do a bunch of different things so first of all we can run the script we can just say python main py then it's going to run the functionality so I can go here into my command line which is just CMD I can navigate to the directory so to this one here and then I can just say Python and my package main py so this would be the running command then I could do some other stuff like I could build the project how do I build the project quite simple python setup Dot py and then I specify builds and S dist underscore oh sorry B dist underscore wheel because we want to have a wheel file with the final build and this creates a build and a disk directory and here we have the wheel file of the project so that is one thing that we can do but then again you know I can also go ahead and delete all of that so I can go ahead and say okay if the directories exist I can just uh call RD slash s slash d uh or actually slash Q sorry on the build directory to remove it I can do the same thing on the disk directory and then those directories are gone so that would be a cleanup and then we can also um install requirements so I can specify here file requirements dot txt I can provide setup tools here as a requirement and you can also provide some other uh packages whatever and in order to install these requirements what we do is we say pip install Dash R requirements txt and then it installs setup Tools in this case which is already installed um and those are the processes that we have here those are the procedures that we would like to have professionally and simply in a make file now why do we want to have that because it's quite tedious to always do the same stuff so let's say I do some changes I make some changes I go into the main file and I don't want to print hello world now I want to print hello world now I changed something I want to rebuild the whole thing what do I have to do first of all maybe I want to run it that's not too complicated I run it I see it works okay now I want to build again so I have to say uh python setup and so on I have to remember the commands and maybe you even have some more complicated procedures and also the cleanup you have to clean up multiple directories you have to also remove this directory here which is also quite tedious to do it every time yourself so you can just have all this under the name of one procedure and this is done using make files that is the purpose of a make file and in order to create a make file we just create here a new file and the important thing is the make file has to be called make file with a capital m so we want to call it make file like this and in this case in pycharm I already have um a plugin that recognizes make files the important thing about make files as far as I understand is that we have to use tabs instead of spaces so when you have something like some command colon and you want to Define what the command is you have to tap into that command you are not allowed to use spaces so you're not allowed to use one two three four it has to be a tap this is important for make files now before we Define the actual make file one thing that's important is uh that you have to be able to run make files now if you have the windows subsystem for Linux that's quite simple because on a Windows subsystem for Linux we can just use make and it is going to look for make files so if I say make it will say no make file found because there is no make file on my desktop if there is a make file it's going to recognize that and execute it on Windows that's not the case by default now in my case it is the case because I already have make installed but by default you don't have make on Windows what you need to do in order to get make on Windows is you either have to go and download the installer you will find hopefully a link in the description down below or the simple way is to install uh I'm not sure what it's pronounced choco Lati or something like that it's written like that choco I think that's that's the name of the tool and once you have this command line tool installed you can just hopefully find a link in the description Down Below on how to get this once you have charcoal installed you can just say choco install and then make and then you're going to be able to use the make command so this is what you want to have in order to be able to execute these um commands here so what we're going to do now is we're going to say that we have a procedure run and the Run procedure is going to just say python my package uh main py then I want to have one install this is just going to say pip install Dash R requirements txt then I want to have one built build is going to be python setup py build B dist underscore wheel and then I want to have one clean for example where I want to say Rd this is on windows by the way Rd slash s slash Q the build that's basically um not exactly the same I think but roughly the same as RM RF on so rm-rf on Linux but you want to do this here for build you want to do this here for distant we want to do this uh what was the name I think now I don't know what the name of the third directory was maybe we have to build it first so let's go ahead python set up py build speed disc underscore wheel then it should create a directories again it's myprojectname.ac info so Rd slash F slash s slash Q my project name dot info and the important thing here on Windows is if you don't want this to crash you need to also say if exists um and then in quotation marks dot slash build if that directory exists only then do all of this because the important thing is if it tries to delete something that does not exist it's going to crash it's not going to do anything and this also means that if you have for example the this directory but the build is already deleted it's not going to execute the second command because you don't have um by the way it's not exists it's exist without the s um but you need to make sure that what you're trying to do here actually works because otherwise it's not going to execute the other command so for example if I if builds does not exist and I'm not checking here then it's also not going to delete this because it's going to crash here that's what I'm trying to say so this is now the make file we can go into our command line on Windows here the CMD command line and I can say now make install and you can see it runs pip install Dash R requirements and then I can do make run and then it says hello worlds um I can also say make built build is up to date uh which basically means that nothing changes if I run build because we already have all of this here um and because of that I can do make clean and make clean is going to delete these directories now you can see they're gone um and then I can say make build and usually oftentimes you have something like make all um if necessary in this case probably doesn't make a lot of sense but you can chain command so if you have multiple things that have to be done in a row you can just say make all and it's going to do all of them in a row all these procedures in a row um but yeah this is how you can automate this and what you can also do here which I didn't do is you can provide also certain files that need to exist so I can say build setup.py and install requirements txt which basically means that this command is not going to be executed if the file does not exist right so that's important as well sometimes now one thing that you might be wondering is um what do I do if I have different commands on Linux and on windows so if I use this make file on Windows it might work but what if I want to use it on Linux on my Linux subsystem for example if I go to um if I go to the directory here I think some of it will work but actually not because uh python python is not the command that we have to use on Linux here it's python3 and it's also not pip it's pip3 and it's also not Rd slash s slash Q it's rm-rf so how do I change the commands based on the operating system quite simple we can go up here and we can say if equals so if EQ and then dollar and Os in parentheses comma windows underscore NT that basically means if the operating system is Windows then do all of this Define all of this otherwise we can then copy all of this here and basically this is now Linux um and mac and other operating systems you can define an end if down here and now we can change the command so Python 3 pip 3 Python 3 and here we can just do rm-rf so actually can I there you go oh actually what do I want to do I want to do let me just do it like that rm-rf RM Dash RF like this so those would now be the Linux commands and if I now go into Linux and I say make run you can see it works I can also say make install and you can see this works I can do make clean works as well and make builds and you can see this works as well and if I now try to do make build again you will see that it says it's up to date nothing changes if I run this again so this is how you professionally use make files in the context of python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye foreign [Music]
Info
Channel: NeuralNine
Views: 40,233
Rating: undefined out of 5
Keywords: python makefile, python make, python, makefiles, make, python makefile windows, windows makefile, chocolatey
Id: Yt-UF7fNLJE
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Tue Jan 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.