question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to call .net core 6.0 dll from python

See original GitHub issue

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.11.0
  • Operating System: Debian GNU/Linux 11 (bullseye)
  • .NET Runtime: .net core 6.0

Details

Goal: attempt to call a simple static function in a .net core dll from a python script

The C# project contains only one file:

namespace Math
{
	public class Calculator
	{
		public static int Factorial(int number)
		{
			int i;
			int fact = 1;
			for (i = 1; i <= number; i++)
			{
				fact *= i;
			}
			return fact;
		}
	}
}

The python script is the following:

from pythonnet import load

load("coreclr")
import clr

reference = clr.AddReference('./Math/bin/Debug/net6.0/publish/Math')
print(reference)
from Math import Calculator

factorial = Calculator.Factorial(6)
print(factorial)
  • If there was a crash, please include the traceback here.
# python factorial.py 
Math, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Traceback (most recent call last):
  File "factorial.py", line 8, in <module>
    from Math import Calculator
ImportError: cannot import name 'Calculator' from 'Math' (unknown location)

To reproduce the environment, here’s a dockerfile:

FROM python:3.11.0-slim-bullseye
    
# Install .Net	
RUN apt-get update
RUN apt-get -y install wget

RUN wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN rm packages-microsoft-prod.deb

RUN apt-get update && \
    apt-get install -y dotnet-sdk-6.0

RUN pip install pythonnet

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
filmorcommented, Nov 23, 2022

The issue is that you have a Math sub folder in the directory that you run factorial.py from. Python’s namespace package loader will take precedence over ours and the respective namespace package is of course empty. Rename the Math directory to something else and it will work:

image

1reaction
0x78f1935commented, Nov 28, 2022

I hate name collisions 😅 , thanks for saving me a headache!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing .net core dll using python
I need to access a C# dll which is built in .net core (.NETCore version = v1.1). I tried in the below way...
Read more >
Using a .NET library (DLL) in python/C++
I have a group of DLL libraries from an application, not developed by me. These libraries can function, as references, in a Visual...
Read more >
How to Combine .NET Core and Python for Seamless ...
In this example, we're calling the Exec method to run a simple Python script that prints “Hello World from Python!” to the console....
Read more >
.NET Core Support · Issue #984 · pythonnet ...
NET into Python. Each of them has a distinct "support" status for .NET Core. The first case should already be possible, as Python.NET ......
Read more >
Anyway to invoke .net core dll from python? : r/dotnetcore
IronPython doesn't support libraries like pandas, matplotlib etc. Any way to call dotnet core dll from CPython?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found