【发布时间】:2015-06-11 04:04:59
【问题描述】:
我在 windows 7/64、matlab 2015a 上使用 winpython (2.7) 和 matlab 的新 python bridge。
>> py.ctypes.util.find_library('c')
ans =
Python str with no properties.
msvcr90.dll
>> py.ctypes.util.find_msvcrt()
ans =
Python str with no properties.
msvcr90.dll
>> py.ctypes.CDLL(py.ctypes.util.find_library('c'))
Python Error: [Error 1114] A dynamic link library (DLL) initialization routine failed
>> x=CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\msvcr90.dll')
Python Error: [Error 1114] A dynamic link library (DLL) initialization routine failed
还会出现一个弹出窗口:
Microsoft Visual C++ Runtime Library
R6034 "an application has made an attempt to load the c runtime library incorrectly"
a couple other SO answers 建议它的 matlab 将其不兼容的 msvc*.dll 副本放在路径上的某个位置,所以我删除了 sys.path 中不是来自 WinPython 的所有内容(只是 matlab 的 \bin\win64\ 和 \Python27\site-packages\ 来自我有另一个 python 安装):
>> py.pprint.PrettyPrinter().pprint(py.sys.path)
['',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\python27.zip',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\DLLs',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\plat-win',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\lib-tk',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\site-packages',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\site-packages\\FontTools',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\site-packages\\win32',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\site-packages\\win32\\lib',
'C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.amd64\\lib\\site-packages\\Pythonwin']
仍然有大量的 msvc*.dll 散布在系统上,而且肯定有一些在 PATH 上:
>> x = py.os.environ
x =
Python _Environ with properties:
data: [1x1 py.dict]
{'TMP': 'C:\\Users\\nlab\\AppData\\Local\\Temp', <<snip>>, 'USERPROFILE': 'C:\\Users\\nlab'}
>> cellfun(@(s)fprintf('%s\n',s),strsplit(char(x{'PATH'}),';'))
C:\Program Files\Haskell\bin
C:\Program Files\Haskell Platform\2014.2.0.0\lib\extralibs\bin
C:\Program Files\Haskell Platform\2014.2.0.0\bin
C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64
C:\Users\nlab\Downloads\opencv\build\x64\vc12\bin
C:\ProgramData\Oracle\Java\javapath
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\bin\
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\libnvvp\
C:\Program Files\ImageMagick-6.8.3-Q16
C:\Program Files (x86)\OSSBuild\GStreamer\v0.10.7\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.1\\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.1\libnvvp\
C:\Program Files (x86)\PHP\
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Program Files\Intel\DMIX
C:\Program Files\TortoiseSVN\bin
C:\Program Files\SlikSvn\bin\
C:\Program Files\MySQL\MySQL Server 5.5\bin
C:\Program Files (x86)\Common Files\Acronis\SnapAPI\
C:\Program Files (x86)\PostgreSQL\9.2\bin
C:\Python27
C:\Python27\Scripts
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\LilyPond\usr\bin
C:\Program Files (x86)\Git\cmd
C:\Program Files\Microsoft Windows Performance Toolkit\
C:\Program Files\TortoiseGit\bin
C:\Program Files (x86)\QuickTime\QTSystem\
C:\Program Files (x86)\Skype\Phone\
C:\Program Files\Mosek\7\tools\platform\win64x86\bin
C:\Program Files\Haskell Platform\2014.2.0.0\mingw\bin
C:\Users\nlab\AppData\Roaming\cabal\bin
C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell
C:\Gtk+\bin
我注意到在C:\Program Files\MATLAB\R2015a\bin\win64\ 中我们只有msvc[r|p][100|110].dll——这是否意味着它不适用于基于 msvcr90 的 python 发行版,如 winpython 2.7.9.5?
【问题讨论】:
-
msvcr90.dll 的初始化调用
FindActCtxSectionString在当前激活上下文中寻找“msvcr90.dll”。如果此调用失败,则卸载,返回FALSE,加载程序报告初始化失败。通常对于 Python 2.7,此激活上下文由嵌入在 python.exe 中的清单提供。但是,您也可以在运行时修改激活上下文。请参阅我对How do I load a C DLL from the SXS in Python? 的回答。 -
感谢@eryksun,这看起来很有希望。但是我不熟悉这些东西,你能给我一些背景吗?就像我试图将 python 指向 matlab 的 msvcr 或 v/v 一样,为什么他们不能各自对自己感到满意?他们中的一个人正在使用它不期望的 msvcr 会很危险吗?我的清单应该是什么样的?您的 .py 代码不需要更改,对吗?我真的很想从 matlab 中正常工作,他们的安装很复杂,所以我不能真正编辑他们的 cdll 调用 -- github.com/Toblerity/Shapely/blob/master/shapely/libgeos.py
-
显然我必须使用matlab的——用
CDLL('C:\\Program Files\\MATLAB\\R2015a\\bin\\win64\\msvcr100.dll')替换shapely的CDLL(find_library('c'))使它一切正常——所以@eryksun,如果我理解,你的方法将是一种确定正确路径的方法自动使用? -
嗯,一位身材匀称的开发人员建议只使用
ctypes.cdll.msvcrt——可行——我认为这应该等同于find_library('c')?无论如何,那么你不需要任何花哨的清单/激活上下文的东西...... -
这取决于它使用 CRT 做什么。例如,使用不同的 CRT 打开需要与解释器和其他扩展模块共享的文件(即
FILE *和底层文件描述符)是错误的。但如果它在没有崩溃的情况下工作,那么它可能没问题。
标签: python matlab ctypes msvcrt msvcr90.dll