MongoDB with Async Python Transcripts
Chapter: Foundations: Pydantic
Lecture: Project Setup
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, that's enough talking. Let's write some code and play around with Pydantic.
0:06
And this is our very first chance to write some actual code in this course. So let's just take an extra moment and get the repository all set up.
0:17
Over here, I've got the code repository as it would be checked out from GitHub.
0:22
What I want to do is open this in PyCharm and have it create a virtual environment.
0:29
Now PyCharm can do that itself or you could create your own virtual environment and work with whatever editor you like.
0:37
So I'm just going to go the PyCharm way. Now on macOS, I can drag and drop this folder onto PyCharm and it'll open it.
0:45
On Windows or Linux, you have to say file, open folder, and then browser. So drop it in like this. Here our project is open.
0:56
You can see down in the right that it's detected the git branch and so on. But the most important thing is what version of Python is running.
1:03
It says 3.11, that is great, but this is the global 3.11. So let's quick add a local interpreter.
1:10
Now normally I would go here and just say, add interpreter, add local interpreter, but for some reason PyCharm like literally doesn't respond.
1:18
So I'll just go over here to the settings interpreter and say add interpreter this way. a new virtual environment right in that location
1:26
based on Python 3.11. And here we go. Now you can see the virtual environment has got the name, the MongoDB devs in it.
1:35
That means it's a isolated virtual environment, not just the global Python interpreter. So while that's a lovely readme, we don't need that.
1:44
Let's go over to the code and we'll make a new directory. And this directory is gonna be named after the chapter that we're on,
1:52
we're on chapter four, Foundations of Pydantic, call it something like that. And let's add a Python file just called first_pydantic.
2:03
This is gonna be our first exploration with Pydantic. I'll let Git add that. Now to work with Pydantic, we're going to need to import it.
2:13
And I'm gonna be super explicit here. I'm going to use the Pydantic module as a namespace and not just imported the things from there.
2:23
So you can see exactly where these elements are coming. So we'll say import Pydantic. Now you can see that stopped auto completing there.
2:32
What we need to do is we need to add Pydantic as an external package. And notice you can see this requirements.in
2:39
that is not yet quite added to Git, but now it is. And in here, normally we have a requirements.txt file,
2:47
but I'm gonna use pip tools to actually allow us to more carefully manage the dependency. So into this requirements.in,
2:56
we write only our top level packages without version. So pydantic for the moment. And then we're going to use that to generate a requirements.txt file.
3:08
So make sure your virtual environment is active here. And this here, we'll first pip install pip tools.
3:17
That's where the tools we're gonna need for this. And also we want to run that. Excellent. And so now we can say pip-compile requirements.in --upgrade.
3:32
The upgrade doesn't matter the first time, but the second time it'll have an effect. What this is gonna do is actually generate the requirements.txt.
3:40
And we can close that up and go here. Let's add that Git as well. And look what it's done. It says you wanted Pydantic
3:53
because you asked for it in the requirements. And this is the latest version at the moment. And Pydantic itself depends upon typing extensions.
4:02
And its current latest version is this. This will right now, while it's quite simple, but it will grow in complexity as we build up
4:10
working with things like FastAPI and Beanie and all of those, it's going to be much more interesting as well.
4:16
In PyCharm, we'll install these if we click here, you can click that if you want, or you can say pip install -r requirements.txt
4:25
as you typically would. Alright, looks like everything is happy. And now we can import Pydantic.
4:31
Now let's define a method here, we could just write the code right into the just inline as a script without any functions.
4:39
But I kind of like to have some structure to this. So we're gonna have a main method and it'll do whatever. And then we'll do the dunder name thing.
4:50
And again, since this is only the very first one, let's go ahead and just make some output happen.
4:55
All right, I'm going to run this, right click, run Pydantic. See, it's using our virtual environment Python and we get Hello World, fantastic.
5:05
From now on, we can press this button or on macOS hit Control + R or whatever hotkey you see up there as you hover over it.