Movatterモバイル変換


[0]ホーム

URL:


Choose a framework to optimize documentation to:
    Python

    Using the Python Runtime with Vercel Functions

    The Python runtime is availableinBeta onall plans

    The Python runtime enables you to write Python code, including usingDjango andFlask, with Vercel Functions. You can use a specificPython version as well as use arequirements.txt file toinstall dependencies.

    You can create your first function, available at the/api route, as follows:

    api/index.py
    from http.serverimport BaseHTTPRequestHandlerclasshandler(BaseHTTPRequestHandler):defdo_GET(self):        self.send_response(200)        self.send_header('Content-type','text/plain')        self.end_headers()        self.wfile.write('Hello, world!'.encode('utf-8'))return
    A hello world Python API using Vercel functions.

    By default, new projects will use the latest Python version available on Vercel.

    Current available versions are:

    You can specify which of the available Python versions to use by definingpython_version inPipfile:

    Pipfile
    [[source]]url="https://pypi.org/simple"verify_ssl= truename="pypi"[packages]flask="*"[requires]python_version="3.12"

    An examplePipfile generated withpipenv install flask.

    Thepython_version must exactly match one of theoptions above or it will be ignored. When upgrading to3.12, ensure you set Node.js20.x or22.x in yourprojectsettings.

    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.

    You can install dependencies for your Python projects by defining them inrequirements.txt or aPipfile with correspondingPipfile.lock.

    requirements.txt
    Flask==3.0.3

    An examplerequirements.txt file that definesFlask as a dependency.

    For basic usage of the Python runtime, no configuration is required. Advanced usage of the Python runtime, such as with Flask and Django, requires some configuration.

    The entry point of this runtime is a glob matching.py source files with one of the following variables defined:

    • handler that inherits from theBaseHTTPRequestHandler class
    • app that exposes a WSGI or ASGI Application

    Python uses the current working directory when a relative file is passed toopen().

    The current working directory is the base of your project, not theapi/ directory.

    For example, the following directory structure:

    directory
    ├── README.md├── api|  ├── user.py├── data|  └── file.txt└── requirements.txt

    With the above directory structure, your function inapi/user.py can read the contents ofdata/file.txt in a couple different ways.

    You can use the path relative to the project's base directory.

    api/user.py
    from http.serverimport BaseHTTPRequestHandlerfrom os.pathimport joinclasshandler(BaseHTTPRequestHandler):defdo_GET(self):        self.send_response(200)        self.send_header('Content-type','text/plain')        self.end_headers()withopen(join('data','file.txt'),'r')as file:for linein file:            self.wfile.write(line.encode())return

    Or you can use the path relative to the current file's directory.

    api/user.py
    from http.serverimport BaseHTTPRequestHandlerfrom os.pathimport dirname, abspath, joindir=dirname(abspath(__file__))classhandler(BaseHTTPRequestHandler):defdo_GET(self):        self.send_response(200)        self.send_header('Content-type','text/plain')        self.end_headers()withopen(join(dir,'..','data','file.txt'),'r')as file:for linein file:            self.wfile.write(line.encode())return

    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 asSanic.

    Instead of defining ahandler, define anapp variable in your Python file.

    For example, define aapi/index.py file as follows:

    api/index.py
    from sanicimport Sanicfrom sanic.responseimport jsonapp=Sanic()@app.route('/')@app.route('/<path:path>')asyncdefindex(request,path=""):returnjson({'hello': path})

    An exampleapi/index.py file, using Sanic for a ASGIapplication.

    Insiderequirements.txt define:

    requirements.txt
    sanic==19.6.0

    An examplerequirements.txt file, listingsanic as a dependency.

    Last updated on June 26, 2025

    Previous
    Node.js
    Next
    Go

    Was this helpful?

    supported.
    On this page

    [8]ページ先頭

    ©2009-2025 Movatter.jp