- Notifications
You must be signed in to change notification settings - Fork749
Description
Hi, thanks for the great library! It has been tremendously helpful in both my personal and professional life!
Right now I am encountering a problem that might not be pythonnet specific but is not obvious to solve and encountered when I use Pythonnet. Specifically, when my project references SkiaSharp which contains native dlls, pythonnet will not be able to load everything properly - it locates the entry assembly fine, but the dependencies are not loaded. The initial import is ok but during runtime an exception is thrown.
Would love to hear any suggestions on solving this problem.
Environment
- Pythonnet version: 3.0.5
- Python version: 3.11.9 64bit
- Operating System: Windows 7 64-bit
Below is the entry assembly, which directly references SkiaSharp.
<ProjectSdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReferenceInclude="SkiaSharp"Version="3.119.0" /> </ItemGroup></Project>
usingSkiaSharp;namespacePythonNetSkiaSharpProblem{publicclassProgram{publicstaticvoidMain(string[]args){try{varinfo=newSKImageInfo(width:64,height:64);usingvarsurface=SKSurface.Create(info);SKCanvascanvas=surface.Canvas;canvas.Clear(newSKColor(red:0,green:200,blue:200,alpha:255));usingSKImageimage=surface.Snapshot();usingSKDatapngData=image.Encode(SKEncodedImageFormat.Png,quality:100);conststringoutputPath="filled64.png";usingvarstream=File.OpenWrite(outputPath);pngData.SaveTo(stream);System.Console.WriteLine($"Saved{outputPath}");}catch(Exceptione){Console.WriteLine(e);}}}}
And this is how I am referencing the entry assembly,from some location outside the published folder (notice the same problem happens even if this script is placed directly inside the published folder).
importsyssys.path.insert(0,r"C:\Users\Charles Zhang\Desktop\Temp\PythonNetSkiaSharpProblem\PythonNetSkiaSharpProblem\bin\Debug\net8.0")importpythonnetpythonnet.load("coreclr")importclrclr.AddReference("PythonNetSkiaSharpProblem")fromPythonNetSkiaSharpProblemimportProgramProgram.Main([])# Exception in stack
Output:
System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp' or one of its dependencies: The specified module could not be found. (0x8007007E) at SkiaSharp.SkiaApi.sk_colortype_get_default_8888() at SkiaSharp.SkiaApi.sk_colortype_get_default_8888() at SkiaSharp.SKImageInfo..cctor() --- End of inner exception stack trace --- at SkiaSharp.SKImageInfo..ctor(Int32 width, Int32 height) at PythonNetSkiaSharpProblem.Program.Main(String[] args) in C:\Users\Charles Zhang\Desktop\Temp\PythonNetSkiaSharpProblem\PythonNetSkiaSharpProblem\Program.cs:line 11
This is likely due to this folder structure:
└───net8.0 │ PythonNetSkiaSharpProblem.deps.json │ PythonNetSkiaSharpProblem.dll │ PythonNetSkiaSharpProblem.exe │ PythonNetSkiaSharpProblem.pdb │ PythonNetSkiaSharpProblem.runtimeconfig.json │ SkiaSharp.dll │ └───runtimes ├───osx │ └───native │ libSkiaSharp.dylib │ ├───win-arm64 │ └───native │ libSkiaSharp.dll │ ├───win-x64 │ └───native │ libSkiaSharp.dll │ └───win-x86 └───native libSkiaSharp.dll
See:https://github.com/Charles-Zhang-Python/PythonNetSkiaSharpProblem
This might be related to#468 but is somewhat different since I am not loading the dll directly, and everything is .Net 8 properly.