Python for .NET “unable to find assembly” error

前端 未结 6 1751
情书的邮戳
情书的邮戳 2020-12-10 11:42

I\'m using CPython and I have a C# dll. I\'m trying to use Python for .NET to make them talk. I can\'t use IronPython because I need to integrate this into an existing CPy

相关标签:
6条回答
  • 2020-12-10 11:56

    Did you try 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 it works, but this code works on my computer (Python 2.7, .NET4)

    0 讨论(0)
  • 2020-12-10 11:57

    Is DotNet4Class.dll built against .NET 4? I assume so based on the naming of the dll.

    Note the issue 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'd read the solution, but essentially, you need to rebuild and recompile the python for .NET project under .NET 4.

    I'll also mention that projects like this, that aren't actively developed and used by lots of people, generally have subtle idiosyncrasies that make knowledge of the platform essential to work around problems such as this. It sounds like you're trying to hack this solution in without understanding much about python or .NET which is always going to be fraught with problems.

    0 讨论(0)
  • 2020-12-10 11:59

    I have code like this (I copied MyRightClickMenuService.dll to the same directory as my script.py). It is built against .Net 4.0.

    # script.py
    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
    )
    
    0 讨论(0)
  • 2020-12-10 12:02

    One reason can be Windows was not enabling it to load from "external sources". To fix this:

    • Right-click on the .dll
    • "Properties"
    • Under "General", click "Unblock"
    0 讨论(0)
  • 2020-12-10 12:16

    Checklist

    1. The folder(s) containing the DLL(s) is/are added to sys.path before loading. You may append, or sys.path.insert(0, dll_folder) to put it first on the list.
    2. You call clr.AddReference('my_dll') without the dll extension (for my_dll.dll), after adding the folder to sys.path
    3. The DLL Target Architecture is the same as the CPython version bitness. That is, if Architecture is x64, use 64-bit python, and if Architecture is x86, use 32-bit python. (instructions for this below)

    How to check target Architecture for DLL?

    • I Used ILSpy (free and open source) -> Open DLL -> Check the output. Below example output.

    0 讨论(0)
  • 2020-12-10 12:17

    Try this (without extension .dll):

    clr.AddReference(r"C:\pyfornet_test\DotNet4Class")
    
    0 讨论(0)
提交回复
热议问题