我试图在python中导入和使用DLL。因此我正在使用pythonnet。
import sys
import clr
sys.path.append('C:\PathToDllFolder')
clr.AddReference('MyDll.dll')但是,代码会产生以下错误:
回溯(最近一次调用):文件"E:\NET\NET_test.py",第6行,在clr.AddReference( 'MyDll.dll‘) System.IO.FileNotFoundException中:无法找到程序集’MyDll.dll‘。bei Python.Runtime.CLRModule.AddReference(字符串名)
DLL的目标运行时为: v4.0.30319
有没有办法找出导入失败的原因以及我如何修复它?
(如果有必要,我也可以提供DLL)
发布于 2018-07-26 01:52:38
对我来说是这样的。Dll位于‘/SDK/dll/Somenet64.dll’注意:不需要.dll扩展。
import os, sys, clr
dll_dir = './SDK/dll/'
dllname = 'some_net64'
path = r'%s%s' % (dll_dir, dllname)
sys.path.append(os.getcwd())
clr.AddReference(path)发布于 2020-07-10 14:40:04
clr.AddReference()不能很好地描述错误。了解导入失败的更好方法是使用此方法。
#use this section of code for troubleshooting
from clr import System
from System import Reflection
full_filename = r'C:\foo\bar\MyDll.dll'
Reflection.Assembly.LoadFile(full_filename) #this elaborate the error in details一种可能是系统知道您的DLL是从其他地方下载的(甚至Dropbox同步计数),并且不允许您使用该DLL文件。在这种情况下,您可以从https://learn.microsoft.com/en-us/sysinternals/downloads/streams下载一个工具并运行此命令来从DLL文件中删除所有这些标志。
stream -d MyDll.dll在此之后,带有clr.AddReference()的导入应该可以工作。
发布于 2018-05-16 15:59:26
在python字符串中,"\"是转义字符。要在python字符串中真正使用反斜杠字符,需要添加第二个字符:"\\"。
将sys.path.append('C:\PathToDllFolder')更改为sys.path.append('C:\\PathToDllFolder')。
我不确定clr.AddReference('MyDll.dll'),没有.dll的版本应该能工作:clr.AddReference('MyDll')
https://stackoverflow.com/questions/49998309
复制相似问题