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