Rock Solid Python with Python Typing Transcripts
Chapter: Frameworks Built on Typing
Lecture: pip-tools for Adding Requirements

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now to get started, we need to install Pydantic into our virtual environment. Remember at the beginning,
0:08 I added this virtual environment called Python type hints. If we open our terminal, you can see that it is active right there
0:15 and which version of Python it is. Now, of course I could just say pip install Pydantic, that's how it works,
0:21 but I want to make sure that we have a repeatable and safe way, a stable way of adding requirements
0:29 and dependencies 'cause as we go through this chapter and subsequent one, there's gonna be a lot of things we're going to be working with.
0:35 So I'm gonna use this thing called pip tools. Now let me give you a quick preview of how this works. So typically, you might go to the PyProject toml,
0:44 you might go like with flit or poetry. I like to keep things more simple and straightforward, just the built-in stuff.
0:51 So I'm gonna use requirements.txt. And over here, you would have something like Pydantic. Now to be safe, you need to say Pydantic,
1:00 you know, whatever version 2.0.4, whatever the current version is. And you probably also need to figure out the dependencies of Pydantic.
1:10 Set those in here as well. So instead of going through this process, I'm gonna use something called pip tools. And I'm gonna call this pip tools.
1:20 It could be called anything, it doesn't matter. But I like to call it pip tools, and then it generates the,
1:26 pip-tools will generate the requirements.txt. Check this out. Make sure we have this installed. It does, if you don't have it,
1:34 just pip install pip-tools. So the command we wanna run is pip compile, giving it this input that says, here are top level dependencies,
1:46 generate the requirements files pinned, and hey, if there's a new version of not just identic, but one of its dependencies that matches,
1:53 that still satisfies the requirements, you know, like whatever Pydantic restrictions might have on them, then also upgrade those. So I'll run this.
2:02 You do not need to do this 'cause the file you need will already have been created right here. So notice it says we're gonna require Pydantic 241,
2:12 which is apparently the current version because we set it here. But look, we also need annotated types because Pydantic needs it.
2:18 And here's its pinned version. The core is needed for Pydantic. That's why here and this one is required by both of those and it's pinned and so on.
2:28 So the last thing to do is pip install -r requirements like so. You'll see me sometimes write PIR, I have a alias for that,
2:41 but you can type it out if you wish. All right, now this warning should go away and if you go over here and say pip list,
2:48 you'll see things like Pydantic, okay? I wanna walk you through that Just so you know what the deal is with these files,
2:54 again, you do not need to have pip-tools, you do not need to run pip compile, you just need to pip install -r requirements.txt.
3:02 But now you know where they come from, why it's here, so on. This is a primo way to manage your requirements.
3:09 Wanna update them, just run that --upgrade and it'll figure out what the newest version of everything can be. Highly recommend pip-tools.


Talk Python's Mastodon Michael Kennedy's Mastodon