A Sample Python Microservice using FastAPI
Before you begin, ensure you have the following installed:
- Python 3.11 or higher
- pip (Python package installer)
- pipenv
To create an environment from scratch, you can run the following command in your terminal:
python3 -m venv .venv
followed by activating the environment:
source .venv/bin/activate
Alternative way of creating an environment is to use the makefile:
make env
The make file will tell you how to activate the environment. In my case it printed out:
[i] To activate the virtual environment, type:
source pythonmicroservice.venv/bin/activate
Note
You should activate your virtual environment before installing dependencies.
Install FastAPI:
pip install "fastapi[all]"
Install all other requirements:
pip install -r requirements.txt
If you used the command:
make env
all of the requirements should already be installed for you. Just make sure that you have activated the environment.
To start the server, you can run the following command in your terminal:
uvicorn src.main:app --reload
Or you can use the makefile target
make run
Documentation is available at:
or