Using Makefile in Python with a Virtual Environment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey in this video I'm going to show you how to use make files with python it's somewhat uncommon but actually really really nice these tools go together nicely I'll share with you why I think this is a good idea I'll show you how to do it how to set up a virtual environment how you can kind of do this in vs code and if you stay all the way to the end I'll give you a sense of of why I think these are great tools that really come together nicely really I think that using make files with python does make some things really nice and it works everywhere it's really a great combination alright so here is my Python program what I'm going to do is just show you how I run it test it install the dependencies whatever using the command line here in vs code down at the bottom and then I'm going to show how to do the same thing in make files and you're going to see that it's better or easier or or just more fun I don't know but but here's my program I have this app.pi and it calls this API get fact before which it asks you to enter a number here's my API right it Imports requests it gets a fact about a number and it calls this URL it calls this numbers API assuming it gets a 200 back it gives the response back to you um yeah so that's the program it's pretty simple but but it's a little bit fun so I have to add into my requirements that I need this request library and then I can run it so the first thing I need to do to run it is install my requirements with Pip and then they're installed and then I can run my program and then it just asks you for a number I'm going to put in 123. 123 is the atomic number of the yet to be discovered element your bitrium interesting did not know that let's try this again same number 123 is the electricity emergency telephone number in Indonesia and this is just how the numbers API works it returns facts about numbers we can try much smaller ones right like three is the number of performers and a trio an interesting thing about Python and using make files is make files are normally used for compiled code right it's usually a build tool that you use to build things and python is an interpreted program you don't need to compile it you just run it or is it right you can see that when I ran this this Pi cache was created and it was actually compiled this is the python C code right here the line between interpreted code and compiled code is actually it's a murky line it's not clear what is what and you can use make files for interpreted languages all the time that's what I'm going to show you so so far we we installed our requirements and we ran the program we might also want to clean up by removing that pie cash folder then we just have our our normal stuff there okay so now you kind of understand the actions that I might be taking as part of this project let's put them in a make file so uh the way that make files are structured is you have a Target name in this case my my target name is setup then you have a list of prerequisites here things that have to happen first and then you have a recipe which is kind of a multi-line list of steps that need to happen so my target here named setup it has a prereq on this requirements file which is where these dependencies are and then it runs pip install the reason it lists requirements here as a dependency is because if I add a new file to requirements then we need to run the setup again to install it next I have my run step so for run I have a prerequisite which is the setup because I need to install all this stuff before I run it and then I just run it and then clean is removing my pie cache so that I can clean any of that out down here you can see that I list run and clean as phony steps that's because in a make file there is an expectation that the target name is going to correspond to a file this target name here corresponds to the file requirements.txt and if I were to add a file run here I don't want mcfile to get confused that this run is referring to that file instead I say it's a phony Target it doesn't actually refer to a file um yeah the other thing I have up top here is the default goal this is just a way to say if you just run make without specifying anything then it's going to run this run step which is pretty convenient because it's telling people how to use your project you as a python developer might know hey yes I want to run python uh in order to run this app but which which file do you start it with and if they're not a python developer maybe they don't know yeah so the defaults are pretty helpful I think so that's the first version um but actually I don't just want to run pip install globally I I would like to use a virtual environment so I'm going to start and create my virtual environment I'm going to make my activate script executable then I'm going to Source the activate script and now you can see I am inside of my virtual environment when I'm inside it right then obviously I can install all my requirements then inside that environment I can run my script there are four types of blood so let's take that and let's adapt that to a make file so we want to use a virtual environment and install all our dependencies inside of there so there's actually a couple ways to do this I'm going to cover both ignoring the top here the first thing that I've done is I've set up a target for the activate script and I've said the activate script depends on requirements so when the activate script is called we are going to create a new environment we are going to cmod.file we are going to activate it by sourcing that file and then we're going to do our pip install requirements so it's the exact same steps as before that will do our initial virtual environment creation if it already exists and we want to just create it we have this vmf target so the VM Target will just do the sourcing of that file which will get us inside of the virtual environment which means that when we run our run if we make it depend on vmf which is depend on setting up this virtual environment then we can run our app inside of the environment so here on Mac OS the default version of make is pretty old it's 3.81 make uh if you're greater than 3.82 I believe it has this command uh or this I don't know is it a command I don't know anyways you can say one shell when you say one shell it changes so that each of these recipes rather than being activated uh run in its own sub process is all run in the same one which is actually what we want right because when we activate here our virtual environment we want the run down here to be inside of that same shell right so one shell is the command we need to give to make to make that happen if however you like me have a very old version of uh make then this one shell will just be ignored and it will run them each in a separate thing in which case we can use a little trick which is we just make sure that we use the right pythons and Pips so here I've extracted my python to refer to the version that will be inside of my virtual environment and the same for pip right so here even if the one shell doesn't hit we're still referring correctly to the right pip and the right python and also an even better way than that is what I did is I just did Brew install make and that gives me gmake which is the later version right there I have 4.4 and so if I do G make run we can see that it activates our virtual environment and then it runs the program which we can run like that and same goes for make because we've wrapped our python we can do make run same thing that will happen right but but our virtual environment's already been activated at this point so just to prove to you this actually works let's clear it or clean it uh as the name might be and then we're going to run our make run you can see there was no virtual environment so we have to create it right which will take a second then it's installing all the requirements you can see they're all being installed into our environment and then we can run like that and then the second time that we run that you see it doesn't have to do it because it's already activated and the same uh will happen if we use our g-make which is you know just better uh in all ways it's a problem I've been bit by before uh many times the version of a Unix tool on Mac OS is just kind of old uh there we go so yeah you want to if you're if you're using virtual environments if you're using python you want to use a make file this is all you need to know yeah use this one shell use a you know fairly recent version of make you can set a default goal so that when I just do make it will run it without any specific input and yeah followed this overall process and you'll have a great make file and it just makes working with a python project even easier that's kind of why I like make files you can use them with any language you can use them you know to do things like this and you can use them just for simple task Runners right if you just have a step like to run a test or to print something out or look at a log file easy to put it in a make file and then it's just tracked there you have a nice central place where you list all the things you work on and you know like you learn make once and you know don't create the super largest hairiest McFall ever because they can get a bit painful but that's a skill that'll pay dividends because make has been around forever and it will continue to to be around and it works great with whatever tool you're using I like this vs code has this nice make file Runner if you want buttons rather than command line you know you can set up here uh what do I want so my build process I'll just call my build process run and then if I use this button then you can see this is running here there we go all of a sudden I am able to use these buttons like it's an IDE but it's just a make file so that is make files in Python I think they're great if you want to learn more about software development you know subscribe to this Channel or just come back I work for Earthly if you like make files Earthly is a build tool it's kind of like Docker for builds it lets you run your CI builds locally as well as in whatever environment in CI it's very nice and very cool and open source probably works with your existing CI and if you like make files I think you're gonna like it so check that out at earthly.dev or you know down in the description you'll find a link to it yeah so that's my video thank you very much
Info
Channel: Earthly
Views: 8,317
Rating: undefined out of 5
Keywords: makefile, makefile python, makefile django, python makefile, makefile python tutorial, python makefile tutorial, make command, example django program, sample django project, django python, django tutorial, python django, django 2.0, django 2, beginners tutorial, python tutorial, python django 2, python tutorial for beginners, django framework, python tutorials, python django projects, python, makefile tutorial, simple makefile, linux
Id: w2UeLF7EEwk
Channel Id: undefined
Length: 12min 4sec (724 seconds)
Published: Tue Jan 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.