You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker环境下使用pythonnet报错:无法创建默认.NET运行时

Pythonnet引用DLL在Docker中运行失败:无法创建.NET运行时

问题背景

我尝试通过Pythonnet的clr模块引用DLL实现文件格式转换,编写了对应代码并配置了Dockerfile,但运行容器时始终报错:先是无法创建默认的mono运行时,尝试切换到coreclr后仍失败,试过多种方案均未解决。

代码示例

import clr

clr.AddReference(dll_path)
import RxLibrary

# 转换为ASC格式
input_file = f"./{filename}.rxd"
output_file = f"./{filename}.asc"
RxLibrary.RxLib.ConvertData(input_file, output_file)

Dockerfile示例

# 使用官方Python镜像作为基础
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 复制当前目录内容到容器
COPY . /app

# 安装依赖包
RUN pip install --trusted-host pypi.python.org boto3 python-dateutil asammdf pytz pythonnet

# 暴露端口80
EXPOSE 80

# 设置环境变量
ENV NAME World

# 容器启动时运行脚本
CMD ["python", "./test3.py"]

报错信息

初始报错

RuntimeError: Failed to create a default .NET runtime, which would
                    have been "mono" on this system. Either install a
                    compatible runtime or configure it explicitly via
                    `set_runtime` or the `PYTHONNET_*` environment variables
                    (see set_runtime_from_env).

切换到coreclr后的报错

RuntimeError: Failed to create a .NET runtime (coreclr) using the
                parameters {}.

解决方案

方案1:安装Mono运行时(适用于.NET Framework DLL)

官方Python镜像未预装Mono,需在Dockerfile中添加安装步骤:

FROM python:3.9-slim

# 安装Mono运行时及开发包
RUN apt-get update && apt-get install -y mono-runtime mono-devel && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY . /app

RUN pip install --trusted-host pypi.python.org boto3 python-dateutil asammdf pytz pythonnet

EXPOSE 80
ENV NAME World

CMD ["python", "./test3.py"]

同时在Python代码中显式指定使用Mono运行时:

import clr
# 显式设置使用mono运行时
clr.set_runtime("mono")

clr.AddReference(dll_path)
import RxLibrary

# 转换代码
input_file = f"./{filename}.rxd"
output_file = f"./{filename}.asc"
RxLibrary.RxLib.ConvertData(input_file, output_file)

方案2:使用.NET Core Runtime(适用于.NET Core/.NET 5+ DLL)

若你的DLL是.NET Core兼容版本,安装.NET Runtime并配置环境变量:

FROM python:3.9-slim

# 安装.NET 6 Runtime(可根据DLL兼容版本调整)
RUN apt-get update && apt-get install -y wget && \
    wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
    dpkg -i packages-microsoft-prod.deb && \
    apt-get update && apt-get install -y dotnet-runtime-6.0 && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY . /app

RUN pip install --trusted-host pypi.python.org boto3 python-dateutil asammdf pytz pythonnet

# 设置Pythonnet使用coreclr
ENV PYTHONNET_RUNTIME=coreclr
# 指定runtimeconfig路径,替换为实际安装的版本号
ENV PYTHONNET_CORECLR_RUNTIME_CONFIG=/usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.25/runtimeconfig.json

EXPOSE 80
ENV NAME World

CMD ["python", "./test3.py"]

或在代码中显式指定路径:

import clr
# 替换为实际的runtimeconfig路径
clr.set_runtime("coreclr", runtime_config="/usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.25/runtimeconfig.json")

clr.AddReference(dll_path)
import RxLibrary

# 转换代码
input_file = f"./{filename}.rxd"
output_file = f"./{filename}.asc"
RxLibrary.RxLib.ConvertData(input_file, output_file)

内容的提问来源于stack exchange,提问作者Suprava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 02:20:57