1

I'm attempting to create a simple Python script that calls a .Net function through Python.Net (http://pythonnet.github.io/). I'm trying to configure a Docker image containing both Python, .Net and, of course, Python.net, possibly at their latest releases.

I attempted in many ways, starting from several base machines (Ubuntu, Debian, Alpine, ...). I managed to install Python (I tried with several flavours and versions) and .Net (or Mono), but, every time I get stuck when installing Python.Net.

I get errors like those discussed in Pip Pythonnet option --single-version-externally-managed not recognized or in Error Installing Python.net on Python 3.7 (suggested solutions in those posts didn't work for me).

As an example, here's a (failing!) 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

Another attempt is:

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}

The dockerfile above builds correctly, but when running the container with a /bin/bash, issuing:

conda activate myenv
python myscript.py

I get an error like:

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)

(solutions found around the Internet, like https://github.com/pythonnet/pythonnet/issues/1034#issuecomment-626373989 didn't work, neither did attempts to use different versions of Python and .Net)

To be noted that the same error occurs also replacing .Net with Mono.

How can a docker image be built, that can run a simple Python script that makes use of Python.Net?

Starnuto di topo
  • 3,215
  • 5
  • 32
  • 66

0 Answers0