Skip to content

clr.AddReference fails on macOS and Linux with .NET 9 but works on Windows #2514

@manu-aesim

Description

@manu-aesim

Environment

  • Pythonnet version: 3.0.4
  • Python version: 3.11.2
  • Operating System: macOS (M1 Pro), linux (x86 and ARM)
  • .NET Runtime: 9.0

Details

It seems that there is a compatibility issue with .NET 9 on macOS and Linux. Calling 'clr.AddReference(path)' fails on these platforms but works on Windows when the library is compiled with .NET 9 and the .NET runtime version is set to “9.0.0”.

Traceback (most recent call last):
  File "/Users/manu/Source/pythonnet_minimal/pythonnet_minimal.py", line 10, in <module>
    clr.AddReference(dll_filepath)
System.IO.FileNotFoundException: Could not load file or assembly '/Users/manu/Source/pythonnet_minimal/bin/Debug/net9.0/pythonnet_minimal.dll, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: '/Users/manu/Source/pythonnet_minimal/bin/Debug/net9.0/pythonnet_minimal.dll, Culture=neutral, PublicKeyToken=null'
   at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Python.Runtime.AssemblyManager.LoadAssembly(AssemblyName name) in /home/benedikt/dev/pythonnet/dist/.tmpXppz4v/pythonnet-3.0.4/src/runtime/AssemblyManager.cs:line 234
   at Python.Runtime.CLRModule.AddReference(String name) in /home/benedikt/dev/pythonnet/dist/.tmpXppz4v/pythonnet-3.0.4/src/runtime/Types/ClrModule.cs:line 109
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)

I have created a simple repo that reproduces this issue: https://github.com/aesim-tech/pythonnet_minimal. You can check the action that runs on Windows and fails on Linux.

Note: The same code works correctly on macOS and Linux when targeting .NET 8.0.

Steps to Reproduce

  1. Clone the repository: git clone https://github.com/aesim-tech/pythonnet_minimal.git
  2. Build the .NET project: dotnet build
  3. Run the Python script: python pythonnet_minimal.py

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