Setup PDM and FastAPI
Step 2 in the series of building a FastAPI web service with Python, I'll show you how to set up PDM and FastAPI to get us to a "Hello World" state.
I have set up a Github repo so you can follow along easily. The plan is to have branches corresponding to each section of this Substack. For this section, refer to the `step-2-the-project-setup`
branch.
Setup PDM
Recently, I learned about PDM for managing Python dependencies, akin to how Yarn and Bundler work for their respective languages. I appreciate this pattern as it allows the definition of build scripts and the integration of additional tooling.
Install PDM
Several methods to install PDM include using brew, pip, and piping to bash. However, given that we've already used ASDF to install Python, let's stick with ASDF for PDM to maintain consistency.
asdf plugin add pdm
asdf install pdm 2.10.4
asdf local pdm 2.10.4
# to update PDM you can run
pdm self update
which pdm
/Users/tom/.asdf/shims/pdm
Optional: Add completions to your shell
The PDM documentation provides examples for adding shell completions. Since I'm using Oh My Zsh (OMZ) and prefer standard setups, let's proceed with the OMZ method:
mkdir $ZSH_CUSTOM/plugins/pdm
pdm completion zsh > $ZSH_CUSTOM/plugins/pdm/_pdm
Initialize PDM for our project
For this new project, initialize PDM using `pdm init`
. Follow the prompts to set up your Python environment and project settings.
pdm init
# Follow the interactive setup here
Creating a pyproject.toml for PDM...
Please enter the Python interpreter to use
0. /Users/tom/.asdf/shims/python (3.11)
1. /Users/tom/.asdf/installs/python/3.11.0/bin/python3.11 (3.11)
2. /Users/tom/.asdf/shims/python3.11 (3.11)
3. /Users/tom/.asdf/shims/python3 (3.11)
4. /Users/tom/.asdf/installs/python/3.11.0/bin/python (3.11)
Please select (0): 0
Would you like to create a virtualenv with /Users/tom/.asdf/installs/python/3.11.0/bin/python? [y/n] (y):
Virtualenv is created successfully at /Users/tom/Workspace/the-full-stack/.venv
Is the project a library that is installable?
If yes, we will need to ask a few more questions to include the project name and build backend [y/n] (n):
License(SPDX name) (MIT):
Author name (YOUR_NAME):
Author email (YOUR_EMAIL):
Python requires('*' to allow any) (>=3.11):
Project is initialized successfully
🤯💥 that was easy…
Now, your project will have a `pyproject.toml`
file for managing dependencies. TOML files have gained popularity in the industry for configuration purposes. I also like that the T stands for Tom 🤣.
Adding Dependencies
Let's add some essential dependencies to set up our application:
# Linting
pdm add -dG style "black"
# Environment Management
pdm add python-dotenv
# Framework
pdm add fastapi
# ASGI Server
pdm add uvicorn
This should get us started. If you cloned this repo, you can run `pdm sync`
to install the dependencies like this.
Add a Simple Main App
Add a `main.py`
file to the `src/the_full_stack`
directory in the project and define a basic Hello World endpoint.
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
Testing Our Setup
Run uvicorn
server installed above with this command
uvicorn main:app --reload --app-dir ./src/the_full_stack
Yay! this worked. Let’s add this command to our `pyproject.toml`
file for easier reuse later.
[tool.pdm.scripts]
_.env_file = ".env"
uvicorn = "uvicorn main:app --reload --app-dir ./src/the_full_stack"
Now, we can run the app with a simple `pdm run uvicorn`.
pdm run uvicorn
INFO: Will watch for changes in these directories: ['/Users/tom/Workspace/the-full-stack']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [91931] using WatchFiles
INFO: Started server process [91934]
INFO: Waiting for application startup.
INFO: Application startup complete.
Summary
We've successfully set up our local development environment and a basic application that responds to GET
requests. This setup is a solid starting point for any app you need to build. In future posts, I'll show you how to create a standardized platform for all your applications.