How to use Python.Included library within Docker container?

  .net, asp.net, docker, python

I’ve got an ASP.NET Core API server. I’m using some python libraries via Python.Included.
When I build a Dockerfile and run it, it complains about being unable to load shared library ‘python37’. When PythonEngine.Initialize() is run, an error is thrown.

Error message when docker build: System.DllNotFoundException: Unable to load shared library ‘python37’ or one of its dependencies.

TargetFrameworknet: 5.0

DockerDefaultTargetOS: Linux

Python.Included: 3.7.3.13

Code snippet:

  Installer.SetupPython();
  Installer.TryInstallPip();
  PythonEngine.Initialize();

Sample Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["TestProject.csproj", "."]
RUN dotnet restore "./TestProject.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "TestProject.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestProject.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
RUN apt-get update 
    && apt upgrade -y 
    && apt-get install -y python3.7

COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestProject.dll"]

Source: Docker Questions

LEAVE A COMMENT