在 docker 镜像linux 容器中安装 Python.Net

问题描述

我正在尝试创建一个简单的 Python 脚本,该脚本通过 Python.Net (http://pythonnet.github.io/) 调用 .Net 函数。 我正在尝试配置一个包含 Python、.Net 和 Python.net 的 Docker 映像,可能是它们的最新版本。

我尝试了多种方式,从几台基本机器(Ubuntu、Debian、Alpine 等)开始。 我设法安装了 Python(我尝试了多种风格和版本)和 .Net(或 Mono),但是,每次我在安装 Python.Net 时都卡住了。

我遇到了类似 Pip Pythonnet option --single-version-externally-managed not recognizedError Installing Python.net on Python 3.7 中讨论的错误(这些帖子中建议的解决方案对我不起作用)。

举个例子,这是一个(失败的!)dockerfile:

FROM python:slim-buster

# Install .Net  
RUN apt update
RUN apt install wget -y
RUN wget https://dot.net/v1/dotnet-install.sh
RUN chmod 777 ./dotnet-install.sh
RUN ./dotnet-install.sh -c 5.0

#install Python.Net
RUN pip install pythonnet

另一种尝试是:

FROM continuumio/anaconda

# Install .Net  
RUN apt update
RUN apt install wget -y
RUN wget https://dot.net/v1/dotnet-install.sh
RUN chmod 777 ./dotnet-install.sh
RUN ./dotnet-install.sh -c 5.0

#install Python.Net

# See: https://stackoverflow.com/a/61948638/1288109
RUN conda create -n myenv python=3.7
SHELL ["conda","run","-n","myenv","/bin/bash","-c"]
RUN conda install -c conda-forge pythonnet

ENV PATH=/root/.dotnet/:${PATH}

上面的 dockerfile 构建正确,但是当使用 /bin/bash 运行容器时,发出:

conda activate myenv
python myscript.py

我收到如下错误

Traceback (most recent call last):
  File "factorial.py",line 1,in <module>
    import clr
ImportError: System.TypeInitializationException: The type initializer for 'Sys' threw an exception. ---> System.DllNotFoundException: /opt/conda/envs/myenv/lib/../lib/libmono-native.so assembly:<unkNown assembly> type:<unkNown type> member:(null)

(在 Internet 上找到的解决方案,例如 https://github.com/pythonnet/pythonnet/issues/1034#issuecomment-626373989 不起作用,尝试使用不同版本的 Python 和 .Net 也不起作用)

需要注意的是,将 .Net 替换为 Mono 也会发生同样的错误

如何构建一个 docker 镜像,它可以运行一个使用 Python.Net 的简单 Python 脚本?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)