Before you can start building APIs with FastAPI, you need to set up your development environment. This involves ensuring you have the correct Python version and installing the necessary libraries: FastAPI itself and an ASGI server like Uvicorn to run your applications. We'll also establish a best practice by using a virtual environment to manage project dependencies cleanly.
This course assumes you have Python 3.7 or later installed. FastAPI leverages modern Python features like type hints and asynchronous programming, which require these recent versions. You can check your Python version by opening your terminal or command prompt and running:
python --version
# or sometimes python3 --version
If you need to install or update Python, please visit the official Python website (https://www.python.org/) and follow the instructions for your operating system.
It's highly recommended to use a virtual environment for each Python project. Virtual environments create isolated spaces where you can install specific versions of packages without affecting your global Python installation or other projects. This prevents dependency conflicts and makes your project reproducible.
Python includes the venv
module for creating virtual environments.
Navigate to your project directory: Open your terminal and change to the directory where you want to create your FastAPI project.
mkdir fastapi-ml-intro
cd fastapi-ml-intro
Create a virtual environment: Run the venv
module, giving your environment a name (a common convention is .venv
or venv
).
python -m venv .venv
# or python3 -m venv .venv
This command creates a .venv
directory containing a copy of the Python interpreter and a place to install project-specific packages.
Activate the virtual environment: Activation modifies your shell's path to prioritize the environment's Python interpreter and packages.
source .venv/bin/activate
.venv\Scripts\activate.bat
.venv\Scripts\Activate.ps1
(You might need to adjust your execution policy first: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
)Once activated, your terminal prompt will usually show the environment's name (e.g., (.venv) your-user@your-machine:...$
). All subsequent pip
installations will happen within this isolated environment. To deactivate, simply type deactivate
.
With your virtual environment activated, you can now install FastAPI and Uvicorn using pip
, Python's package installer.
Install both with a single command:
pip install "fastapi[all]"
The [all]
option is a convenience provided by FastAPI. It installs fastapi
itself, uvicorn
for serving, pydantic
for data validation (which FastAPI heavily relies on), and several other optional dependencies that are often useful (like python-multipart
for form data, jinja2
for templating, etc.). This ensures you have a comprehensive starting point.
If you prefer a minimal installation, you could install them separately:
pip install fastapi uvicorn pydantic
However, using fastapi[all]
is generally recommended for starting out.
To confirm that FastAPI and Uvicorn were installed correctly, you can ask pip
to show their details:
pip show fastapi uvicorn
This command will display information about the installed packages, including their versions. Ensure no errors are reported.
While not strictly required, using a good code editor or IDE (Integrated Development Environment) will significantly improve your development experience. Options like Visual Studio Code (VS Code), PyCharm, Sublime Text, or others offer features like syntax highlighting, code completion, debugging, and terminal integration, which are very helpful when working with frameworks like FastAPI.
Your development environment is now ready. You have Python, FastAPI, Uvicorn, and an isolated virtual environment. In the next section, you'll use this setup to create and run your first simple FastAPI application.
© 2025 ApX Machine Learning