Modern Python Projects Transcripts
Chapter: Let's build a package
Lecture: Add code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we have the cookie cutter template set up,
0:03 let's copy code from the previous chapter.
0:06 So I have one VSCode instance open with this new
0:10 python package that we are creating.
0:12 And I have another one with this code that we wrote in the previous chapter.
0:17 This one is not in the full screen mode,
0:18 so we can easily distinguish with trying to switch.
0:21 So let's start with the main code.
0:23 Since the cookie cutter template is suggesting that we use a separate file for the CLI
0:27 we'll go with that. This will provide a nice separation between the CLI functions
0:33 and other helper functions. And one important advice that I have for you here is
0:38 don't name any files with the same name as the name of the package.
0:43 So, we have the package called uptimer and inside we have the uptimer.py
0:47 We got away with that in the previous chapter.
0:51 But if we have a package and inside we have python file of the same name
0:55 it will be confusing when you try to import commands from there.
1:00 When you type from uptimer import something,
1:03 do you mean from the uptimer file or from the uptimer package,
1:07 Especially since we have the relative and absolute imports in Python.
1:11 It will be confusing to some people or even to you in the future.
1:16 So let's just rename this file to better reflect what it's supposed to do,
1:20 and we're going to call it helpers.py
1:23 Okay, so let's go to the old code.
1:33 And from here we will copy the first two functions and put it into the helpers
1:38 folder, well helpers file.
1:45 We don't need this. And then let's take the last function and let's add it
1:53 to the CLI file. Let's keep the name main for the main function, and main here
2:06 Actually, let me reuse this code because I like the system exit.
2:13 What I will do. I'll just copy this.
2:26 Rename it, and I also need some imports.
2:35 I dont need request and let me sort the imports because I'm pretty sure they are
2:39 wrong. No, they actually are not.
2:44 And we have to import functions from the helpers, let's sort again,
3:02 so let's test manually. If this works,
3:05 let's go back to the terminal.
3:08 First things first. We have to create a new virtual environment.
3:16 If we go back to the VSCode,
3:17 it will detect this virtual environment.
3:19 So we want to use it, now back to the terminal.
3:23 So now we actually have to install our dependencies.
3:26 The click package, the request and so on?
3:29 How do we do this? Well,
3:30 we could create a requirements file.
3:33 But when you are creating a python package,
3:35 you will also have to specify dependencies inside the setup.py file.
3:40 So by specifying them instead of .py and inside the requirements.txt,
3:46 you're basically duplicating your work. So to avoid that,
3:49 we're going to keep creating the requirements.txt file.
3:53 But we'll keep the requirements dev,
3:55 because those are the requirements that I will be using when working on the package.
4:00 But the end users of this package don't really need it.
4:03 So inside the setup.py,
4:05 you can see that we have the requirements variable,
4:08 and here we have to add any other requirements that are needed to run my
4:13 uptimer command in the terminal. So let's go back to the old code and let's
4:18 see what other requirements we had there.
4:22 Let's go back here and let's copy all that and let's temporary paste it here.
4:28 So Python obviously doesn't have to be specified here.
4:31 Flake is okay. lets add requests lets add colorama and the last two
4:55 We don't really need it here,
4:56 but we're going to need it in the requirements .dev.
4:59 So let's take care of that now, in the requirements dev,
5:16 I'm specifying the exact versions of my dependencies.
5:19 That's because I want to have reproducible builds.
5:22 So when I switched to a different computer and I want to work on my uptimer
5:26 package, I want to have the exact same versions of packages that I used
5:31 before. On the other hand,
5:33 in the setup.py,
5:34 I'm specifying the minimal requirements for my package.
5:38 If there is a new version of Click released,
5:41 I will anyway tested before I publish it on pypi.
5:45 So, there is no risk that updating click to version 8 will break my uptimer
5:51 for the end users because I will have to first try it out.
5:54 And if it works, then I will have to build it with the click 8 included.