Modern Python Projects Transcripts
Chapter: Managing Python project
Lecture: Makefiles - simple tasks management
0:00 When we talked about the simple project, I showed you Makefile, Makefile is a
0:05 file containing a set of instructions for a make build automation tool.
0:10 For example, if you go to see python repository,
0:13 you'll see a Makefile there.
0:18 You can use it to build cpython on your computer from the source.
0:22 And as you can see, it's huge.
0:25 Back in the days it was more popular to build packages on Linux,
0:29 but now we have other tools, that make installing packages much easier.
0:33 But quite often, even though you don't see the make command being run,
0:37 it's actually run in the background, for a long time
0:41 I was scared of Makefiles because I only use them to build packages on Linux
0:45 So those Makefiles that I saw, were long and full of bash commands that
0:50 I didn't understand. On top of that,
0:52 if some dependencies were missing that makes command would fail and I had to search on
0:57 stackoverflow, what random package I have to install to make it work.
1:01 So my first impression was that Makefiles are complex,
1:04 scary, and I don't want to use them,
1:07 but then I work on some projects that used to Makefiles for much smaller task
1:11 And I realized that you can write a useful Makefile without knowing anything about
1:16 bash scripting. I don't want to go too deep into the topic of Makefiles
1:20 but I just want to show you that they're quite easy to use.
1:24 They are a perfect solution. If you need to group some commands that you would
1:28 run in your terminal, they're also useful.
1:30 If you're passing a lot of parameters to a function, instead of remembering all those parameters
1:34 you can write them down in the Makefile.
1:37 Let me show you an example of a simple Makefile.
1:42 At the top. We have .PHONY.
1:45 This is an optional instruction, but it's quite important one.
1:49 If by any chance, you have a file called. Let's Say init in your
1:53 folder and you run, make init, make will try to run that init file
1:58 instead of executing the init command from this Makefile.
2:02 So, to prevent that for each command that you specify in a Makefile,
2:06 you should add it to this .PHONY instruction and then you define Make commands, you
2:11 specify the command name:. And then in the next line,
2:15 you write down what commands should be executed.
2:18 To run those commands in your terminal,
2:20 you just need to type, make and the name of the command, something that is
2:24 very important and probably confusing to Python programmers, is that Makefile you tabs,
2:29 not spaces. So if you run into a error saying missing separator stop,
2:34 it means that your Makefile contains four spaces instead of a tab, that can happen
2:39 usually when you copy and paste code from somewhere.
2:42 But when you write your Makefiles by hand and you press the tab key,
2:45 you should be fine. Our first task is called Help,
2:49 and it's used to simply print the instructions on how to use this,
2:52 Makefile. If I run make help.
2:55 It will print those five commands,
2:56 explaining what each of those commands do.
3:00 Next, We have init command.
3:02 Its main purpose is to set up everything for your application.
3:05 So, if a new developer comes to your project,
3:08 you can tell them. Just run,
3:09 make init command in your terminal,
3:11 and you will have the whole application, up and running in seconds, as you can see
3:16 this init command is running to other make commands, build and run,
3:21 but it's also running some other shell commands.
3:24 Build and Run both run some docker commands.
3:27 Don't worry. If you don't know what they do,
3:29 I will cover some basics of Docker at the end of this course.
3:33 And finally, we have two commands related to test.
3:37 The first one simply runs pytest on the tests directory.
3:41 So, if you run make test in your terminal,
3:44 it will in turn, run pytest tests command.
3:47 If later you decide to use a different framework for your test,
3:51 you just need to modify the test command in the Makefile.
3:54 Your end users will still run.
3:56 Make test, so you don't even have to update the documentation.
4:00 That's another great advantage of using Makefiles.
4:03 They standardize a set of commands between projects.
4:06 If I go to a random project on GitHub and I see that it has
4:10 a Makefile, I'm like 99% sure that if I run make test,
4:14 it will run tests for that project.
4:17 I don't even need to know what testing framework this project is using. I have also
4:22 put here one more test command.
4:24 This one is running pytest,
4:26 but with a bunch of additional options and parameters.
4:29 So in this case, I want to run pytest and set the output very
4:33 verbose. That's the -vv parameter.
4:38 I want to Only run test marked as unit,
4:41 and I don't want to generate a coverage report, if that's a command that I run
4:45 often, then instead of remembering all those options,
4:49 I can just define a task called unittest and then just run make unittest
4:54 It's much less typing and much less memorization.
4:58 If you want to try using Makefile in your next project,
5:01 you might be wondering how to get it.
5:03 If you're on Linux, chances are that you already have it installed because make is
5:07 often required to install some packages.
5:10 If not, you can easily find on the Internet how to install it,
5:14 In Ubuntu, You just need to run,
5:15 apt-get install make. If you are on Mac,
5:19 you can, either install it through the xcode by running,
5:22 xcode select --install. Otherwise,
5:25 you can use homebrew and run brew install make, if you're on Windows,
5:29 make will be available when you install the windows subsystem for Linux,
5:33 which is a very popular tool, if you're a developer working on Windows.
5:38 Otherwise you can install cygwin. That will give you a lot of Linux tools.
5:43 And if you use the chocolatey package manager,
5:46 you can just run chocol install make,and this will also work.