我正在try 首次部署一个简单的FastAPI应用程序来vercel. Vercel.json就在下面.

{
  "devCommand": "uvicorn main:app --host 0.0.0.0 --port 3000",
  "builds": [
    {
      "src": "api/index.py",
      "use": "@vercel/python",
      "config": {
        "maxLambdaSize": "15mb",
        "runtime": "python3.9"
      }
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "api/index.py"
    }
  ]
}

我已将运行时指定为python3.9,但这并没有反映实际运行时,该运行时仍然是python3.12(默认).这最终会导致内部错误.

如何正确配置运行时版本?

我还看了官方docs,上面说builds房产不应该使用.所以我try 像下面那样重写.

{
  "devCommand": "uvicorn main:app --host 0.0.0.0 --port 3000",
  "functions": {
    "api/index.py":
    {
      "runtime": "python@3.9"
    }
  },
  "routes": [
    {
      "src": "/(.*)",
      "dest": "api/index.py"
    }
  ]
}

这也不起作用. 也许我不应该在Python项目中使用vercel?(互联网上信息很少)

推荐答案

我已经滚动浏览了vercel文档,但找不到任何可以在Vercel.jsonbuildsfunctions部分中指定Python版本的引用.

  • 在"构建"中,您指定nPM包@vercel/python.我想这更像是node.js运行python3脚本的界面.
  • 在"functions"中,您可以指定无服务器函数.我不确定这是否是您需要的.

参考编号 : https://vercel.com/docs/projects/project-configuration

Solution:

然而,正如Vercel文档中列出的那样,Python版本可以定义为Pipfile.

参考编号 : https://vercel.com/docs/functions/runtimes/python

注意,Vercel仅支持pony 3.12(默认)和pony 3.9(需要遗留图像,即使用Node.js 16或18.)

Vercel Manuals:

This links might be useful for setting up your first python project on vercel: https://github.com/vercel/examples/tree/main/python/hello-world
https://github.com/vercel/examples/tree/main/python/flask3
https://github.com/vercel/examples/tree/main/python

What worked for me

首先创建您的http handler或flottle app或其他:

## my_app.py
from http.server import BaseHTTPRequestHandler, HTTPServer
import sys

class GETHandler(BaseHTTPRequestHandler):
 
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/plain')
        self.end_headers()
        self.wfile.write('Hello, world!\n'.encode('utf-8'))
        python_version = f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}"
        self.wfile.write(f'Python version {python_version}'.encode('utf-8'))

# variable required by Vercel
handler = GETHandler

接下来创建一个Pipfile.此bash命令将自动创建Pipfile.

## run in bash
pip install pipenv
pipenv install

自动生成的pipfile看起来应该是这样的:

## Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pipenv = "~=2023.12"

[dev-packages]

[requires]
python_version = "3.9"

接下来创建package.json.您需要将node.js版本设置为18.x才能在Python 3.9中运行脚本.

## package.json
{
    "engines": {
      "node": "18.x"
    }
    
}

第三,您需要定义将触发执行Python服务器的路由:

## vercel.json
{
"builds": [
    { "src": "*.py", "use": "@vercel/python" }
  ],
  "redirects": [
    { "source": "/", "destination": "/my_app.py" }
  ]  

}

最后,让我们设置部署设置.在我的项目设置中,我不会添加任何运行或构建命令:

enter image description here

然而,对于python 3.9,请判断您的node.js版本是否为18.x(同一页面,即在部署设置中).

enter image description here

就是这样.部署后,访问生成的路由链接,它会自动将您重定向到/my_app.py.您可以看到使用python3.9来生成页面:

enter image description here

Python相关问答推荐

pandas MultiIndex是SQL复合索引的对应物吗?

使用子字符串动态更新Python DataFrame中的列

修剪Python框架中的尾随NaN值

两极:滚动组,起始指数由不同列设置

"Discord机器人中缺少所需的位置参数ctx

过滤绕轴旋转的螺旋桨

使用regex分析具有特定字符的字符串(如果它们存在)

Pandas 在时间序列中设定频率

如何销毁框架并使其在tkinter中看起来像以前的样子?

如何处理嵌套的SON?

比较两个二元组列表,NP.isin

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

_repr_html_实现自定义__getattr_时未显示

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

为什么默认情况下所有Python类都是可调用的?

基于字符串匹配条件合并两个帧

将pandas Dataframe转换为3D numpy矩阵

numpy卷积与有效

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

如何从pandas的rame类继承并使用filepath实例化