To learn more about the concepts covered in this lesson, check out:
Preparing the Environment
00:12 Then you’ll create a virtual environment and install all the project dependencies that you need for the project. Finally, you’ll learn how to store environment variables outside of your code and how to load the variables into your application. First, you’ll create the project’s structure.
00:45 You’ll run the commands for your application from the root folder that you’re in right now. The files and folders that you’ll create will be located in either this folder or your apps folder, which will be created next.
Note that without a
__init__.py file, you’d create not a regular package, but a namespace package. A namespace package comes in handy when splitting the package over multiple directories, but you are not splitting the package in this project.
Check out this Real Python course to learn more about packages. Dependencies are the Python packages that the FastAPI project needs to work. Before installing them with
pip, it’s a good idea to create a virtual environment. That way, you are installing the dependencies not systemwide, but only in the specific project environment.
and here are the commands for Windows Terminal. First, you create a virtual environment named
venv by using Python’s built-in
venv module. Then you activate it with the source command or the activation script, depending on the platform that you’re on. The parentheses surrounding the virtual environment name indicate that you successfully activated the virtual environment.
You’ll use the FastAPI web framework to build the API, so it’s no big surprise that
fastapi is the first requirement of the project. To run the API, you’ll need a web server. That’s what
uvicorn is for.
Web server gateway interfaces specify how your web server communicates with your web application. Traditional implementations like
gunicorn need to run multiple processes to handle network traffic concurrently.
04:15 ASGI, in contrast, can handle an asynchronous event loop on a single thread as long as you can avoid calling any blocking functions. FastAPI leverages the ASGI standard, and you can use the Uvicorn web server, which can handle asynchronous functionality. But as you’ll see in this course, you don’t have to write asynchronous code to use FastAPI.
If you want to learn more about asyncio in Python and how FastAPI handles parallelism, then you can check out the concurrency and
await package of the FastAPI documentation at the link seen on-screen.
04:57 With the web framework and web server installed, next is SQLAlchemy. SQLAlchemy is a Python SQL tool kit that helps you communicate with your database. Instead of writing raw SQL statements, you can use SQLAlchemy’s object-relational mapper (ORM).
05:15 The ORM provides a more user-friendly way for you to declare the interactions of your application and the SQLite database that you’ll use. Your application will also rely on environment variables.
As the name suggests, the
validators library helps you validate values like email addresses, IP addresses, or even Finnish social security numbers. In your project, you’ll use
validators to validate the URL that a user wants to shorten.
06:24 Now that you’ve created a virtual environment and installed the Python packages needed for the project, in the next section of the course, you’ll see how to use environment variables with your application, making its deployment easier and more secure.
Become a Member to join the conversation.