Vercel.json setup for FastAPI - positioning the root folder

Hello, I’m trying to set up my FastAPI app on the Hobby plan and running into some issues which I think are easy to solve but I have little Vercel experience.

My repository has the following (somewhat atypical) structure

/api
   /routes
   /models
   connection.py
   main.py
/... other folders
.env
README.md
requirements.txt
vercel.json

So important here is that main.py isn’t in the root directory (but in the /api directory). To make sure Vercel correctly picks up on where to find it, I have set up vercel.json like this:

{
  "builds": [
    {
      "src": "api/main.py",
      "use": "@vercel/python"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "api/main.py"
    }
  ]
}

However I get the following error screen with **500**: INTERNAL_SERVER_ERROR:
image

And the following log:

[ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'connection'
Traceback (most recent call last):INIT_REPORT Init Duration: 1330.54 ms	Phase: init	Status: error	Error Type: Runtime.Unknown

Inside main.py helper functions from connection.py are imported like this

from connection import get_driver

This all works fine on my local server but I think Vercel has issues with finding connection.py because it goes looking for it in the root directory? I’m not sure though, it’s my best guess. How could I resolve this without moving everything in the /api folder to the root folder?

Literally just had to add . to make sure the path’s relative

from .connection import get_driver
2 Likes

Thanks for coming back with a solution, @robsyc!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.