Skip to content

[Looking for Suggestion] Unable to find assembly: SkiaSharp #2588

Closed
@chaojian-zhang

Description

@chaojian-zhang

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.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="SkiaSharp" Version="3.119.0" />
  </ItemGroup>
</Project>
using SkiaSharp;

namespace PythonNetSkiaSharpProblem
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                var info = new SKImageInfo(width: 64, height: 64);
                using var surface = SKSurface.Create(info);
                SKCanvas canvas = surface.Canvas;
                canvas.Clear(new SKColor(red: 0, green: 200, blue: 200, alpha: 255));
                using SKImage image = surface.Snapshot();
                using SKData pngData = image.Encode(SKEncodedImageFormat.Png, quality: 100);
                const string outputPath = "filled64.png";
                using var stream = File.OpenWrite(outputPath);
                pngData.SaveTo(stream);

                System.Console.WriteLine($"Saved {outputPath}");
            }
            catch (Exception e)
            {
                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).

import sys
sys.path.insert(0, r"C:\Users\Charles Zhang\Desktop\Temp\PythonNetSkiaSharpProblem\PythonNetSkiaSharpProblem\bin\Debug\net8.0")

import pythonnet
pythonnet.load("coreclr")

import clr
clr.AddReference("PythonNetSkiaSharpProblem")

from PythonNetSkiaSharpProblem import Program
Program.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions