Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use .NET dll in Python [duplicate]

I've developed a dll in visual studio that I'd now like to use in Python using the standard IDLE.

I cannot seem to find a straightforward solution to this anywhere. I've tried using pip install *dll location*, but to no luck (hopes were never high).

I've pretty much solely been a .NET developer so my knowledge of python is pretty poor. There must be some way to install third party dll packages.

like image 280
Stinkidog Avatar asked Dec 14 '15 12:12

Stinkidog


People also ask

Can we use C# DLL in Python?

The ATFX API C# DLL files can be used in three supported coding languages: Python, Matlab and LabVIEW. A specific package that allows Python to integrate with . NET Common Language Runtime needs to be installed for the C# DLL files to work in Python.

Can Python use DLL files?

New in Python version 2.5 is the ctypes, a foreign function library. It provides C-compatible data types and allows calling functions in DLLs or shared libraries. Using the ctypes module in Python allows ArcObjects code written in C++ to be used in a geoprocessing script tool.

Can I use .NET with Python?

Python.NET is a package that gives Python programmers nearly seamless integration with the . NET 4.0+ Common Language Runtime (CLR) on Windows and Mono runtime on Linux and OSX. Python for . NET provides a powerful application scripting tool for .

Where do I put Python DLL files?

In the vast majority of cases, the solution is to properly reinstall python. dll on your PC, to the Windows system folder. Alternatively, some programs, notably PC games, require that the DLL file is placed in the game/application installation folder.


1 Answers

Just as a plain and simple answer for others which I was struggling to find.

The dll location needs to be added to the path variable. This can be done simply by importing sys, and invoking the method shown (the path should not include the dll file).

You can then use your dll with Python for .NET (impot clr), by setting up the reference with the AddReference method. Then you're dll is ready to go! An example:

import sys
import clr

sys.path.append(r"C:\Users\...")

clr.AddReference("MyDll")

from mynamespace import myclass

x = myclass()
like image 73
Stinkidog Avatar answered Oct 22 '22 11:10

Stinkidog