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