Preparing the Environment

To learn more about the concepts covered in this lesson, check out:

00:00 Preparing the Environment. In this part of the course, you’ll prepare the development environment for your FastAPI app. First, you’ll create the folder structure for your app.

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:30 You can name the root folder of your project in any way you like, but on-screen, you’ll see it named url_shortener_project. The root folder will be your working directory.

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.

00:57 While you can call your project folder anything you want, it’s important to name your app folder shortener_app. When you’re inside your project folder, create a subfolder called shortener_app.

01:11 The shortener_app folder will be the location that your app will live in. Later on, you’ll add your app’s source code into different files in this shortener_app directory.

01:23 Your URL shortener Python app will be a package named shortener_app that will contain a number of different modules. To use shortener_app as a package, create a file.

01:36 Here’s how it’s done in Windows Terminal …

01:47 and here’s how it’s done in a Linux or macOS terminal.

01:54 This file will stay empty throughout this course. Its only job is to tell Python that your shortener_app directory is a package.

02:05 Note that without a 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.

02:22 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.

02:46 Here are the commands for creating and activating a virtual environment on Linux and macOS …

03:02 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.

03:28 After you’ve created and activated your virtual environment, it’s time to install the dependencies that the URL shortener app requires.

03:39 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.

03:53 It’s a web server implementation for Python that provides an asynchronous server gateway interface.

04:02 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.

04:42 If you want to learn more about asyncio in Python and how FastAPI handles parallelism, then you can check out the concurrency and async / 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.

05:28 You’ll learn more about environment variables later on in the course, but for now make sure to install python-dotenv to load them from an external file.

05:46 Finally, you’ll use a package to validate URLs.

05:54 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:11 Note that the install commands here are singled out to explain why they’re required. You can install all dependencies in one command by chaining the package names.

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.