Skip to main content

Vercel python module import error primary cause

Β· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The primary cause for import errors on Vercel is often a mismatch between your local development environment's flexibility and Vercel's strict, serverless build process. Beyond the crucial __init__.py file, you need to pay attention to your project's overall structure, the way you write imports, and Vercel's build configuration.

1 Relative vs. Absolute Imports πŸ“„β€‹

The way you import files can cause a "command not found" error on Vercel even if your local environment handles it fine.

  • Problem: In your local environment, the root of your project is often automatically added to your Python path, allowing you to use absolute imports like from utils import helper_functions regardless of where the file is. Vercel's environment might not do this, leading to an ImportError because it can't find the utils module [4].
  • Solution: Use relative imports when referencing modules within the same package. For example, if you have a file in api/my_api.py and you want to import from utils/helper_functions.py, you should use a relative import.

Example​

Folder Structure:

my-vercel-app/
β”‚
β”œβ”€β”€ api/
β”‚ β”œβ”€β”€ __init__.py
β”‚ └── my_api.py
β”‚
└── utils/
β”œβ”€β”€ __init__.py
└── helper_functions.py

Correct Import:

# In api/my_api.py
from ..utils import helper_functions

# You can now use helper_functions.my_function()

The .. tells Python to go up one directory level from the current file and then look for the utils package. This ensures your imports are always relative to your package structure, which is a best practice for clean, portable code [3].


2 File and Directory Naming πŸ“β€‹

Vercel has specific conventions for serverless functions, particularly for projects that combine different runtimes, like Python and JavaScript.

  • Problem: Vercel looks for serverless functions in the api/ directory by default [2]. If you have a file structure like this, it can lead to routing conflicts, especially if you're using a framework like Next.js which also uses an api/ folder for its API routes.
  • Solution: For Python-only projects, stick with the api/ directory. For hybrid projects, you might need to use the vercel.json file to explicitly define your function's location and routing.

Example​

// vercel.json
{
"functions": {
"api/my-function.py": {
"runtime": "@vercel/python"
}
},
"routes": [
{
"src": "/api/(.*)",
"dest": "/api/my-function.py"
}
]
}

This configuration tells Vercel that the Python file at api/my-function.py should be handled by the Python runtime and routed to the /api/ path.


3 Build and Deployment Settings πŸ› οΈβ€‹

Vercel's build process is automatic, but sometimes you need to provide explicit instructions.

  • Problem: If you're using a framework that Vercel doesn't automatically detect, or if you have a non-standard file structure, the build might fail, leading to import errors.
  • Solution: In your Vercel project settings, configure the root directory and the build command. The root directory setting tells Vercel where to find your project's code, while the build command can be used to run pre-deployment scripts, like installing dependencies. This is particularly important for monorepos or projects with unique layouts.

Summary: Checklist for Vercel Deployment​

  1. βœ… __init__.py files: Add an empty __init__.py file to any directory you want to import from.
  2. βœ… Relative Imports: Use from . or from .. for imports within your project to ensure they work in all environments.
  3. βœ… File Paths: All relative file paths should be based on the project's root directory, not the serverless function's directory, as Vercel's working directory is the project's base [1, 2].
  4. βœ… vercel.json: If you have a custom folder structure or a hybrid project, use vercel.json to define your functions and their routes explicitly.

Sources​

2 Vercel Docs. "Using the Python Runtime with Vercel Functions." https://vercel.com/docs/functions/runtimes/python 3 Reddit. "Folder Structure Mismatch When Deployed to Vercel." https://www.reddit.com/r/flask/comments/14oqr32/folder_structure_mismatch_when_deployed_to_vercel/ 4 Stack Overflow. "How can I configure vercel.json to accommodate module imports." https://stackoverflow.com/questions/79259815/how-can-i-configure-vercel-json-to-accommodate-module-imports-in-both-my-local-d