【问题标题】:how install shapely on windows so it can be imported from matlab?如何在 windows 上安装 shapely 以便可以从 matlab 导入?
【发布时间】:2015-06-10 21:49:16
【问题描述】:

我在 Windows 7/64 上。 shapely 不在他们的 pypi page 上提供 Windows 安装程序,即使他们 seem to have recently。他们的 pypi 页面和他们的README 说要使用chris gohlke's unofficial binaries。我不清楚原因——像pypi这样的东西不能安装dll,所以如果你的代码依赖于dll,你必须手动制作安装程序,但是制作安装程序很痛苦,因为你需要ms visual studio或什么?

无论如何,gohlke 的二进制文件在 winpython (2.7) 中对我来说很好,但当我尝试使用 matlab 的新 python bridge 导入时却不行。 shapely 是 windows 上唯一在 matlab 中给我带来麻烦的 python 库——我可以使用 openCV、numpy、scipy、scikit's、mosek、picos、PIL、future,一切都很好。它也是我需要 gohlke 安装程序的唯一库。所以我认为有联系。

当我在 matlab 中 py.importlib.import_module('shapely') 时,我得到了这个:

PyException with properties:

ExceptionObject: [1x3 py.tuple]
     identifier: 'MATLAB:Python:PyException'
        message: 'Python Error: [Error 1114] A dynamic link library (DLL) initialization routine failed'
          cause: {}
          stack: [1x1 struct]

还会出现一个弹出窗口:

Microsoft Visual C++ Runtime Library
R6034 "an application has made an attempt to load the c runtime library incorrectly"

谷歌搜索,我明白了 https://msdn.microsoft.com/en-us/library/ms235560(v=vs.90).aspx

Visual Studio 2008。应用程序已尝试加载 C 不使用清单的运行时库。这是一种不受支持的方式 加载 Visual C++ DLL。您需要修改您的应用程序来构建 带有清单。 应用程序必须使用清单来加载 C 运行时库。为了 更多信息,请参阅 Visual C++ 库作为并排共享 Visual Studio 中的程序集和清单生成。 ... 纠正 此错误重建您的应用程序以包含清单。建立一个 带有 Visual Studio 的应用程序会自动将清单放入 生成的 .exe 或 .dll 文件。如果您在命令中构建 行,使用 mt.exe 工具将清单添加为资源。采用 如果您构建一个 .exe,资源 ID 1,如果您构建一个资源 ID 2 .dll。有关详细信息,请参阅如何:将清单嵌入到 C/C++ 应用程序。”

这似乎与 chris gohlke 的 comment here 有关。

这里还有一些我尝试过的东西。

在matlab中:

>> x=py.ctypes.util.find_library('geos_c')

x =

  Python NoneType with no properties.

    None

但在 python 中相同!

>>> from ctypes.util import find_library
>>> print find_library('geos_c')
None

但是,我可以从 python 中匀称地使用:

>>> from shapely.geometry import MultiPolygon, Polygon, Point
>>> Point()
<shapely.geometry.point.Point object at 0x000000000399F2E8>

在matlab中:

>> x=py.ctypes.CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll')

x =

  Python CDLL with no properties.

    <CDLL 'C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll',
handle e3f60000 at 7078b588>

>> x.GEOSversion()
No appropriate method, property, or field 'GEOSversion' for class
'py.ctypes.CDLL'.

>> x.initGEOS()
No appropriate method, property, or field 'initGEOS' for class 'py.ctypes.CDLL'.

>> x=py.ctypes.CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll').GEOSversion()
No appropriate method, property, or field 'GEOSversion' for class
'py.ctypes.CDLL'.

Error: Unexpected MATLAB expression.

>> x=py.ctypes.CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll').initGEOS()
No appropriate method, property, or field 'initGEOS' for class 'py.ctypes.CDLL'.

Error: Unexpected MATLAB expression.

>> py.dir(x)

ans =

  Python list with no properties.

    ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
'__format__', '__getattr__', '__getattribute__', '__getitem__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', '_func_flags_', '_func_restype_',
'_handle', '_name']

在python中:

>>> from ctypes import CDLL
>>> x=CDLL("C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.am
d64\\Lib\\site-packages\\shapely\\DLLs\\geos_c.dll")
>>> print x
<CDLL 'C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\si
te-packages\shapely\DLLs\geos_c.dll', handle e3f60000 at 2292cc0>
>>> x.GEOSversion()
-469489536
>>> x.initGEOS()
36606784
>>> dir(x)
['GEOSversion', '_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__', '
__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__in
it__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__se
tattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_fla
gs_', '_func_restype_', '_handle', '_name', 'initGEOS']

搜索geos_c.dll 只会在我的python 的site-packages 中显示shapely 的目录。

couple otherSO answers 暗示它是 matlab 将其不兼容的 msvcr*.dll 副本放在路径的某处,并且可疑:

>> 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:\\Program Files\\MATLAB\\R2015a\\bin\\win64',
 'C:\\Users\\nlab\\AppData\\Roaming\\Python\\Python27\\site-packages',
 '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']

所以:

>> import py.sys.path
>> path.remove('C:\Program Files\MATLAB\R2015a\bin\win64')
>> 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\\AppData\\Roaming\\Python\\Python27\\site-packages',
 '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']
>> py.importlib.import_module('shapely')

ans = 

  Python module with properties:

                 ftools: [1x1 py.module]
    ctypes_declarations: [1x1 py.module]

    <module 'shapely' from 'C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\lib\site-packages\shapely\__init__.pyc'>

耶!但是一旦我尝试使用它,我就会得到完全相同的 R6034 错误弹出窗口

>> py.shapely.geometry.Point()

触发错误弹出> 10次!

以下内容只执行一次:

>> py.importlib.import_module('shapely.geometry')
Python Error: [Error 1114] A dynamic link library (DLL) initialization routine failed

我还从sys.path 中删除了...\AppData\Roaming\Python\Python27\site-packages,但这并没有改变任何东西。

【问题讨论】:

    标签: matlab manifest ctypes shapely geos


    【解决方案1】:

    我们想使用 matlab 的 msvcr,而不是隐藏它。在匀称,更换 CDLL(find_library('c'))CDLL('C:\\Program Files\\MATLAB\\R2015a\\bin\\win64\\msvcr100.dll') 使一切正常。 this answer 似乎是一种自动确定路径的方法,适用于您从任何地方调用的地方。

    【讨论】:

      猜你喜欢
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多