Using the Python Runtime with Vercel Functions
The Python runtime enables you to write Python code, including using FastAPI, Django, and Flask, with Vercel Functions.
You can create your first function, available at the route, as follows:
The current available version is Python 3.12. This cannot be changed.
You can install dependencies for your Python projects by defining them in a with or without a corresponding , , or a with corresponding .
An example requirements.txt file that defines
FastAPI as a dependency.
An example pyproject.toml file that defines
FastAPI as a dependency.
Vercel Functions support streaming responses when using the Python runtime. This allows you to render parts of the UI as they become ready, letting users interact with your app before the entire page finishes loading.
By default, Python Vercel Functions include all files from your project that are reachable at build time. Unlike the Node.js runtime, there is no automatic tree-shaking to remove dead code or unused dependencies.
You should make sure your or only lists packages necessary for runtime and you should also explicitly exclude files you don't need in your functions to keep bundles small and avoid hitting size limits.
Python functions have a maximum uncompressed bundle size of 250 MB. See the bundle size limits.
To exclude unnecessary files (for example: tests, static assets, and test data), configure in under the key. The pattern is a glob relative to your project root.
Exclude common development and static folders from all Python functions to stay under the 250 MB bundle limit.
FastAPI is a modern, high-performance, web framework for building APIs with Python. For information on how to use FastAPI with Vercel, review this guide.
Flask is a lightweight WSGI web application framework. For information on how to use Flask with Vercel, review this guide.
For FastAPI, Flask, or basic usage of the Python runtime, no configuration is required. Usage of the Python runtime with other frameworks, including Django, requires some configuration.
The entry point of this runtime is a glob matching source files with one of the following variables defined:
- that inherits from the class
- that exposes a WSGI or ASGI Application
Python uses the current working directory when a relative file is passed to open().
The current working directory is the base of your project, not the directory.
For example, the following directory structure:
With the above directory structure, your function in can read the contents of in a couple different ways.
You can use the path relative to the project's base directory.
Or you can use the path relative to the current file's directory.
The Web Server Gateway Interface (WSGI) is a calling convention for web servers to forward requests to web applications written in Python. You can use WSGI with frameworks such as Flask or Django.
The Asynchronous Server Gateway Interface (ASGI) is a calling convention for web servers to forward requests to asynchronous web applications written in Python. You can use ASGI with frameworks such as Sanic.
Instead of defining a , define an variable in your Python file.
For example, define a file as follows:
An example api/index.py file, using Sanic for a ASGI
application.
Inside define:
An example requirements.txt file, listing
sanic as a dependency.
Was this helpful?