Python for .NET "could not find assembly"

I am using CPython and I have a C # dll. I am trying to use Python for .NET to make them speak. I cannot use IronPython because I need to integrate it into my existing CPython system.

I am completely new to Python for .NET, and I actually have very little experience with Python and no experience with C #. So please forgive me if my question seems very simple.

I am using Python 2.7.3 and I downloaded pythonnet-2.0-alpha2-clr2.0_131_py27_UCS2 and unzip it into a folder called pyfornet_test, which also contains the DLL that I am trying to use (DotNet4Class.dll)

Then I run this:

import sys import os import clr sys.path.append(r"C:\pyfornet_test") clr.AddReference("DotNet4Class.dll") 

Which gives me this error:

 System.IO.FileNotFoundException: Unable to find assembly 'DotNet4Class.dll'. at Python.Runtime.CLRModule.AddReference(String name) in C:\Users\Barton\Documents\Visual Studio 2008\Projects\PyShar p\trunk\pythonnet\src\runtime\moduleobject.cs:line 375 

Any advice would be highly appreciated. Thanks!

+13
source share
5 answers

DotNet4Class.dll built on .NET 4? I suppose so based on the name of the DLL.

Check out the problem here: http://sourceforge.net/tracker/?func=detail&aid=3293169&group_id=162464&atid=823891

clr.AddReference fails when assembly is built with.NET 4.0 - ID: 3293169

I would read the solution, but essentially you need to rebuild and recompile the python project for .NET in .NET 4.

I will also mention that such projects, which are not actively developed and not used by many people, usually have subtle features that make knowledge of the platform necessary to solve problems such as this. It looks like you are trying to hack this solution without understanding Python or .NET, which will always be fraught with problems.

+7
source

Try this (without the .dll extension):

 clr.AddReference(r"C:\pyfornet_test\DotNet4Class") 
+10
source

I have such code (I install MyRightClickMenuService.dll in the same directory as my main application). It is built against .Net 4.0.

 import clr import os import sys sys.path.append(os.path.dirname(__file__)) clr.AddReference('MyRightClickMenuService') clr.AddReference('System') clr.AddReference('System.Security') from MyRightClickMenuService import ( AclSecuredNamedPipeBinding, MyMenuItem, MyContextMenuService, etc ) 
+3
source

Have you tried clr.FindAssembly?

 import clr import sys assemblydir = r"C:\pyfornet_test" assemblypath = r"C:\pyfornet_test\DotNet4Class.dll" sys.path.append(assemblydir) clr.FindAssembly(assemblypath) 

I don't know why this works, but this code works on my computer (Python 2.7, .NET4)

+3
source

One of the reasons may be that Windows did not allow booting from "external sources". To fix this:

  • Right click on .dll
  • "Properties"
  • In the "General" section, click "Unblock"
0
source

All Articles