# 安装PyInstaller
pip install pyinstaller
# 打包主程序(示例:main.py)
pyinstaller --onefile --hidden-import=pandas --add-data "model.pkl:." main.py
# 生成的可执行文件位于 dist/ 目录
关键参数说明:
--onefile
:打包为单个可执行文件--hidden-import
:显式声明隐藏依赖--add-data
:添加数据文件(格式:源路径:目标目录)创建 .spec
文件进行精细控制:
# -*- mode: python -*-
a = Analysis(
['main.py'],
binaries=[],
datas=[('config/*.json', 'config')],
hiddenimports=['sklearn.utils'],
hookspath=[],
...
)
# 检查依赖是否完整
ldd dist/main # 查看动态库依赖
# 测试运行
./dist/main --test-mode
// 调用Python可执行文件
var process = new Process {
StartInfo = {
FileName = "/app/python_modules/main",
Arguments = $"--input {inputPath}",
RedirectStandardOutput = true,
UseShellExecute = false
}
};
process.Start();
string jsonResult = await process.StandardOutput.ReadToEndAsync();
Python端(FastAPI示例):
from fastapi import FastAPI
app = FastAPI()
@app.post("/predict")
async def predict(data: dict):
return {"result": model.predict(data["features"])}
.NET 8调用端:
using var client = _httpClientFactory.CreateClient();
var response = await client.PostAsJsonAsync("http://localhost:8000/predict",
new { features = new[] { 1.2, 0.5, 3.4 } });
var result = await response.Content.ReadFromJsonAsync<PredictionResult>();
# 阶段1:构建Python环境
FROM python:3.10-slim as python-builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
COPY . .
RUN pyinstaller --onefile main.py
# 阶段2:构建.NET应用
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS dotnet-builder
WORKDIR /src
COPY *.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app/publish
# 最终阶段
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=python-builder /app/dist/main /app/python_modules/
COPY --from=dotnet-builder /app/publish .
# 安装Python运行时依赖
RUN apt-get update && apt-get install -y \
libgomp1 \
&& rm -rf /var/lib/apt/lists/*
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "YourApp.dll"]
多阶段构建:分离Python和 .NET
构建环境
依赖管理:
slim
镜像减少体积SDK
镜像构建,运行时镜像部署文件组织:
Python
可执行文件存放于 /app/python_modules
# 使用Alpine基础镜像
FROM python:3.10-alpine as python-builder
# 清理缓存
RUN pip cache purge && \
rm -rf /root/.cache/pip
# 创建非root用户
RUN groupadd -r appuser && \
useradd -r -g appuser appuser
USER appuser
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:8080/health || exit 1
# 安装系统级依赖
RUN apt-get update && apt-get install -y \
libgl1 \
libsm6 \
libxext6
# 设置容器locale
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
# 启动容器时映射用户
docker run -u $(id -u):$(id -g) your-image
# 日志持久化
VOLUME ["/app/logs"]
.NET端配置:
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddPrometheusExporter());
Python端配置:
from prometheus_client import start_http_server
start_http_server(8001)
通过 PyInstaller
打包 Python
组件,结合 .NET 8
的强大 Web
能力,最终用 Docker
实现跨平台部署,这种混合架构既发挥了 Python
在 AI/数据处理
方面的优势,又利用了.NET在构建高性能Web服务的特点。
关键注意点:
Python
版本一致Protobuf
代替 JSON
提升性能实际部署时建议使用 Kubernetes
进行容器编排,通过 Ingress
配置统一的 API
网关,实现完整的云原生解决方案。