henon / Python.Included

A Python.NET based framework enabling .NET libraries to call into Python packages without depending on a local Python installation.
MIT License
313 stars 51 forks source link

System.DllNotFoundException in Docker container #30

Closed ArthurBardakov closed 3 years ago

ArthurBardakov commented 3 years ago

Hi. I've got an ASP.NET Core server. When I try to to build and run a docker container, it throws an error on the point of - PythonEngine.Initialize(). System.DllNotFoundException: Unable to load shared library 'python37' or one of its dependencies. Though the python lib is installed. I'm not sure if that's an issue or something to do with conatiner setup, but would be obliged on any insight! Sample Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

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

COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestProject.dll"]
henon commented 3 years ago

It is hard to say, there are multiple ways of using this library if you look at all the different usage examples. Which example is your use case based on?

ArthurBardakov commented 3 years ago

I'm using it to integrate such nlps as stanza and spacy to asp.net api as I don't really know python itself. The thing is it throws the above error just with the snippet installation code I pasted above without installing any libraries for example purposes, just with the basic setup. On PythonEngine.Initialize().

henon commented 3 years ago

Are you using the Python.Included nuget or the Python.Deployment nuget?

henon commented 3 years ago

Also, which OS?

ArthurBardakov commented 3 years ago

I'm using Python.Included. Windows OS. Everything works great when is simply run on vs code, it just happens when a container is run. Without any libs installation. Target framework: .net5. DockerDefaultTargetOS: Linux.

henon commented 3 years ago

Python.Included doesn't work on Linux. You need to check out Python.Deployment for that. Look at the different examples. You need of course to provide a Python distribution that works on Linux etc.

ArthurBardakov commented 3 years ago

Thanks for the advice! I guess I'll try to use Windows as Docker target os.

henon commented 3 years ago

Yeah because Python.Included embeds a python distribution that has been compiled for Windows. The other possibility you have is to install python in your target OS and use Python.Deployment (which embeds no Python) to load it from the installation path.

ArthurBardakov commented 3 years ago

I switched to Windows containers and no longer had the python related problem. It worked, thanks for the tip! But when I tried to install a library, let's say Installer.PipInstallModule("spacy"), and then import it dynamic spacy = Py.Import("spacy") after engine initilize, another error was thrown. Unhandled exception. Python.Runtime.PythonException: ModuleNotFoundError : No module named 'spacy'.. It seems as if it was not even loaded for some reason. Maybe due to container specific environmet. Do you have any idea on why that happens?

henon commented 3 years ago

I guess the PIP install of the library did not work. You can check if the files of the spacy module are there in the embedded python distribution which Python.Included installs into the user directory.

henon commented 3 years ago

I am glad your issue is solved. Can you just write a short conclusion so other users who have the same problem can learn from it?