4

I have a .NET Standard 2.0 class library that I'm trying to use from Python. The library has dependencies on some private and public NuGet packages.

I managed to install a recent dev version of pythonnet from the artifact on appveyor and I can run the following import code:

import clr
import sys
sys.path.append(r'C:\....\My.Library\bin\Debug\netstandard2.0')
ref = clr.AddReference("My.Library")

The code above runs fine, but when I try to import from my modules namespace...

from My.Library import MyClass
# >>> ModuleNotFoundError: No module named 'My'

When I try to do ref.get_ExportedTypes(), I get this:

System.IO.FileNotFoundException:
 Die Datei oder Assembly "Google.Protobuf, Version=3.6.0.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604"
 oder eine Abhängigkeit davon wurde nicht gefunden.
 Das System kann die angegebene Datei nicht finden.
 bei System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
 bei System.Reflection.RuntimeAssembly.GetExportedTypes()

Essentially this is a FileNotFoundException about the NuGet dependency. What is the right way to structure my DLL files / to import it with NuGet dependencies?

thanks

denfromufa
  • 5,610
  • 13
  • 81
  • 138
michaelosthege
  • 621
  • 5
  • 15
  • 1
    I have the same problem and just opened an issue in github: https://github.com/pythonnet/pythonnet/issues/711 – Wayne Aug 04 '18 at 19:37
  • why can't you just download/extract all your dependencies from nuget? are you trying to match Visual Studio experience with C#?! – denfromufa Aug 07 '18 at 18:12
  • I have developed the .NET Standard class library and would like to create a distributable python package that exposes the API of my library to Python users. WIll it be sufficient to put the DLLs of my library and all the NuGet dependencies into the same directory and do `sys.path.append(r'./allthedlls')` ? Some magic helper function that analyzes a DLLs dependencies and automatically downloads & extracts them from NuGet would be awesome though. – michaelosthege Aug 11 '18 at 16:12
  • moving to https://github.com/pythonnet/pythonnet/issues/711 because formatting in Stackoverflow comments is awful. – michaelosthege Aug 13 '18 at 07:21

1 Answers1

3

By putting all dependencies into one directory, the import can work. Several options how to achieve this, such as dotnet publish, are presented in How to get .NET Core projects to copy NuGet references to build output?

michaelosthege
  • 621
  • 5
  • 15