02. Initial Configuration
Managing libraries with pip
Now that we have our folder and the initial files ready (main.py and requirements.txt), we'll start installing the libraries we'll need to work with FastAPI.
This will be the first time we use pip, Python's package manager.
Step 5. Install dependencies with pip
pip is the tool that lets you download and install libraries created by other developers (such as FastAPI, Uvicorn, Pydantic, etc.).
These libraries are stored inside the virtual environment (.venv) to keep your project isolated and tidy.
In your terminal (with the virtual environment active), type the following.
python -m pip install fastapi uvicorn
Analyzing the command, we'll see that:
python -m pipensures that thepipfrom the virtual environment is used, not the system's global one.install fastapi uvicorndownloads and installs the two libraries we'll use to begin:- FastAPI: the framework to build our API.
- Uvicorn: the server that will run the application.
Perfect. So, we can then run another command to verify that it was installed correctly.
python -m pip list
You'll see that dependencies and sub-dependencies were installed. Mainly, note that we have uvicorn and fastapi available.
Test FastAPI and Uvicorn
With the dependencies now installed, let's test our first main.py file.
Run the following command in your terminal:
uvicorn main:app --reload
This means that:
uvicornruns the server.main:appindicates that inside themain.pyfile there is a variable calledapp(our FastAPI object).--reloadrestarts the server automatically every time you save changes.
Open your browser and visit http://127.0.0.1:8000
You'll get something like this:
{
"message": "Hello World"
}
Perfect. It means it's working fine.
Now, open this link.
This means that FastAPI and uvicorn are running together.
At this point, you already have:
- FastAPI and Uvicorn installed correctly.
- The server running and listening on port 8000.
- Your virtual environment active and isolated.
Perfect. Let's move on to the next piece of content, where we'll record the dependencies we have in requirements.txt.