Installing FastAPI
In this tutorial we will learn how to install FastAPI in Linux, Windows or MacOS. FastAPI is fast and robust HTTP server framework for Python.
Before we start:
- You have Python running or your system.
- You have PIP for installing Python packages.
Let’s begin our journey of API development with FastAPI. Before we begin let’s verify that we have Python already install in our system and PIP (Python Package Manager). It is suggest to use Visual Code for writing code, click here to see various open source code editors and how to install them.
Installation through PIP
- Navigate to project folder, for example at Desktop, make new folder fast-api-server-demo.
- Initialize new virtual environment using
python -m virtualenv py3-fastapi
- If you don’t have virtual environment already installed, install it using
pip install vitrualenv
. - Activate using following commands (open command prompt at project folder)
- If you are on Linux & MacOS use:
source py3-fastapi/bin/activate
- And, if you are Windows use:
py3-fastapi/Scripts/activate
- Once activated, we must see
py3-fastapi
context in our terminal.
- If you are on Linux & MacOS use:
def get_csv_file_dialect_columns(file: str, encoding: str):
"""Get csv dialect and number of columns of the csv."""
file_ctx = get_csv_file_context(file=file, encoding=encoding)
with file_ctx as fp:
try:
dialect = csv.Sniffer().sniff(fp.read(1024))
fp.seek(0)
except Exception:
dialect = 'excel' #: can not sniff delimiter, use default dialect
try:
reader = csv.reader(fp, dialect=dialect)
num_columns = len(next(reader))
except StopIteration:
raise IOError('CSV file not exist or is empty')
fp.seek(0)
return dialect, num_columns
Next steps
- Now we will make
main.py
and write our first HTTP server using FastAPI.
Additional Resources:
- Check official documentation at FastAPI.
- YouTube video: How to write API using FastAPI.
Checklist:
- I have installed FastAPI.
- I am ready to make my first project in FastAPI.