Python 项目部署
本篇文档介绍如何在 Rainbond 平台上通过源代码部署 Python 项目。
项目识别
Rainbond 会根据源码根目录中的以下文件识别 Python 项目:
requirements.txtsetup.pyPipfilepyproject.toml
包管理器自动检测
在识别为 Python 项目后,Rainbond 会继续自动检测依赖管理方式:
| 文件特征 | 包管理器 |
|---|---|
requirements.txt / setup.py / 普通 pyproject.toml | pip |
Pipfile | pipenv |
pyproject.toml 中包含 [tool.poetry] | poetry |
environment.yml / environment.yaml / conda.yml / conda.yaml | conda |
注意
environment.yml 仅用于识别 conda 包管理器,不会单独作为 Python 项目的语言识别入口。建议项目根目录仍保留 requirements.txt、setup.py、Pipfile 或 pyproject.toml 之一。
requirements.txt 规范
若无 requirements.txt,可用如下命令生成:
pip freeze > requirements.txt
支持的应用类型
由于 Django 的静态文件支持(CSS、图片等)不是很容易配置而且不方便调试,这里给出一个示例:
| 类型 | 自动识别方式 | 默认启动方式 |
|---|---|---|
| Django | 存在 manage.py | web: python manage.py runserver 0.0.0.0:$_PORT |
| Flask | 识别到 flask 依赖和应用模块 | web: flask --app <module> run --host 0.0.0.0 --port $PORT |
| Uvicorn / ASGI | 识别到 uvicorn 依赖和 app / application 对象 | web: uvicorn <module>:<symbol> --host 0.0.0.0 --port $PORT |
| Gunicorn / WSGI | 识别到 gunicorn 依赖和 wsgi.py / app.py 等入口 | web: gunicorn <module>:<symbol> --bind 0.0.0.0:$PORT |
| Quart | 识别到 quart 依赖和应用对象 | web: hypercorn --bind 0.0.0.0:$PORT <module>:<symbol> |
| Sanic | 识别到 sanic 依赖和应用对象或工厂函数 | web: sanic <module>:<symbol> --host=0.0.0.0 --port=$PORT |
| Aiohttp | 识别到 aiohttp 依赖和 web.run_app() 或工厂函数 | web: python <script> 或 web: python -m aiohttp.web ... |
| Pyramid | 识别到 pyramid 依赖和 *.ini 配置文件 | web: pserve <config.ini> http_port=$PORT |
| Poetry scripts | pyproject.toml 中定义 [tool.poetry.scripts] | web: poetry run <script> |
如果自动识别不符合预期,建议在项目根目录添加 Procfile 明确指定启动命令或者在页面上进行配置,例如:
web: gunicorn demo.wsgi:application --bind 0.0.0.0:$PORT
构建参数
Rainbond 当前支持以下 Python 相关构建参数:
| 参数 | 说明 | 默认值 |
|---|---|---|
| Python 版本 | 3.10、3.11、3.12、3.13、3.14 | 3.14 |
| 镜像源 | 配置 pip 镜像源地址 | https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple |
| 可信主机 | 配置 pip 信任主机 | 空 |
| 启动命令 | 默认会根据项目类型自动识别启动命令,支持在页面上自定义覆盖 | 根据项目类型自动识别 |
部署示例
- 进入目标团队,选择 新建应用 → 基于源码示例
- 选择
python-demo - Rainbond 自动识别为 Python 项目,并自动检测包管理器与启动方式
- 点击构建并部署