9

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.

2
  • @stuartd This looks like what I need thanks. Although when I go to .AddReference('c:/*path to dll*), it throws me an error saying unable to find assembly Commented Dec 14, 2015 at 12:12
  • Make sure you use raw string like r"C:\folder1\test1.dll" or double back-slashes "C:\\folder1\\test1.dll". Commented Dec 14, 2015 at 12:40

1 Answer 1

10

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()
Sign up to request clarification or add additional context in comments.

5 Comments

Where is clr comming from? I get AttributeError: module 'clr' has no attribute 'AddReference'
@Sören Probably python.NET (pip install pythonnet)
This was a great start. I got stuck not knowing what to do after x = myclass() though.
A safe way is to place your DLL in a folder and use pathlib.Path to avoid importing issues
First answer is great but hard coding coding file paths can be an issue if you want to share your code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.