How To Setup A FastAPI Project ?¶
- Create myproject folder and open it with your favorite code editor. We recommend Visual Studio Code.
- Open up your terminal and navigate to myproject directory.
- It is recommended to create a virtual environment for python projects.
- This will isolate your projects and dependencies from each other.
$ cd myproject
# create virtual environment using venv module(-m) and name it as venv-myproejct
$ python3.11 -m venv venv-myproject
- Linux/MacOS users can activate like the following
- Windows users can activate like the following
-
Install FastAPI
-
Install uvicorn For App Server
- Write Project Dependencies To a File
- Usually python app dependencies are written to requirements.txt
- pip freeze command shows how to list installed dependencies
(venv-myproject) $ pip freeze
annotated-types==0.5.0
anyio==3.7.1
click==8.1.5
fastapi==0.100.0
h11==0.14.0
httptools==0.6.0
idna==3.4
pydantic==2.0.3
pydantic_core==2.3.0
python-dotenv==1.0.0
PyYAML==6.0.1
sniffio==1.3.0
starlette==0.27.0
typing_extensions==4.7.1
uvicorn==0.23.1
uvloop==0.17.0
watchfiles==0.19.0
websockets==11.0.3
-
You can write this output list to requirements.txt using the following command:
-
Project Files/Folders Structure
- Create app and tests folders under the myproject folder.
- Create run.py and init.py files under app folder
- Create test_views.py and init.py files under tests folder
-
For now project files and folders structure is like the following.
-
This is the setup for a simple and basic project you can follow.