Thanks to visit codestin.com
Credit goes to github.com

Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chaojian-zhang opened this issue Apr 30, 2025 · 2 comments
Closed

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

chaojian-zhang opened this issue Apr 30, 2025 · 2 comments

Comments

@chaojian-zhang
Copy link

chaojian-zhang commented Apr 30, 2025

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.

@chaojian-zhang
Copy link
Author

chaojian-zhang commented Apr 30, 2025

Notice a workaround is to copy libSkiaSharp.dll directly to the root of the published folder.
But this won't work cross-platform and also if we have many similar libraries that exhibit similar behavior then it's not convenient.

@filmor
Copy link
Member

filmor commented May 2, 2025

  1. Your link is not working
  2. You are creating an executable, not a library
  3. Please try using dotnet publish

@pythonnet pythonnet locked and limited conversation to collaborators May 2, 2025
@filmor filmor converted this issue into discussion #2589 May 2, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants