Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
Lecture: Project setup
0:00 Now for us to build our application, we need to create a new web project. Now, almost every web project that I know of depends on external libraries
0:10 and anytime you have a Python library or Python application
0:13 that depends on external libraries, you're gonna want to start by creating a virtual environment. And of course, this is no different.
0:19 So that's what we're gonna do. here we have the demos, and right now we only have chapter three.
0:23 Of course, all the chapters will be here by the time you're watching the course. Let's go over here, this will let me just pop open a terminal right
0:30 there. And what we're gonna do is we're gonna create a virtual environment. So I'm gonna say "Python3 -m venv venv" and now we're gonna activate it.
0:41 So on Windows, you would activate it just by saying "venv/scripts/ activate" But on Mac and Linux, you say dot, to apply it to this shell,
0:53 then "venv/bin" why is it bin not scripts? Take that up with someone else. I have no idea. But we're gonna activate it like this and you'll see our
1:00 prompt change either way, where it says now you're in this virtual environment. We can ask things like "which Python"?
1:07 Yep, it is the one that we're working with. Another thing we wanna look at is by default, whenever we create a new virtual environment,
1:14 there's a 95% chance that pip itself will be out of date. So let's just go ahead and upgrade that real quick as well.
1:21 I'll just go and do these things next time and not run you through it, but first time through, I want to talk about it. Alright,
1:28 everything looks like it is good and we have our virtual environment. Let's go ahead and just come over here and open this in PyCharm.
1:35 Now on Mac OS, you could drag it onto the icon. on the other OS's, you just go to PyCharm,
1:40 say "file, open directory" or visual studo code, and open that directory as well. Alright, so notice down here, it says "no interpreter".
1:48 There's a chance that it might pick the right one. Let's go and see which one it's after.
1:52 This seems to always change, it's super frustrating, and the way it works, like Sometimes it works and finds the local one we created,
1:59 sometimes it doesn't. This time it didn't. So we're gonna say go to our home directory, go to venv, bin or scripts,
2:05 pick Python. Okay, now it looks like everything is working.
2:11 It's gotta read through Python real quick just to make sure it understands all the types and then we'll be ready to get going.
2:17 Next up, let's go and create a "main.py" That's pretty common in FastAPI to have a main that we're going to
2:23 run, and I'm just going to right click and say "run" to make sure everything's working. Okay, the last thing we need to do is we're gonna need to be
2:30 able to use FastAPI. And if I go and run this again, then it's not so happy about it, right. The last thing we have to do is install FastAPI, and
2:40 we're going to keep track of our dependencies by having a "requirements.txt", and in here, we're gonna put "fastapi", for now,
2:50 we're gonna have a bunch more later, and of course it's suggesting it could install it for us,
2:55 but I'm just going to go to the terminal and show you what we would
2:58 run more generally would say "pip install -r requirements.txt" with the virtual environment active,
3:06 we get all the dependencies of FastAPI at this time. Alright, It looks like everything is good. I think it believes it's misspelled,
3:14 which is unfortunate, but you could tell it to stop showing you that. And let's just do a print, "hello FastAPI" and run this.
3:23 Alright, Perfect. So it looks like we've got our system set up, ready to run Python or running Python 3.9 at the moment.
3:30 If you're unsure which version you got, you can come down here. We have 3.9.0 at the moment,
3:36 but again, anything from 3.6 or beyond should be fine for what we're doing and were able to install and import
3:44 FastAPI. So I think our app is ready to, well, begin writing it actually.