how install shapely on windows so it can be imported from matlab?

424 views Asked by At

i'm on windows 7/64. shapely doesn't offer windows installers on their pypi page, even though they seem to have recently. their pypi page, and their README say to use chris gohlke's unofficial binaries. i'm not clear on the reason -- something like pypi can't install dll's, so you have to make an installer by hand if your code depends on a dll, but making the installer is a pain because you need ms visual studio or something?

anyway, gohlke's binary works fine for me in winpython (2.7), but not when i try to import using matlab's new python bridge. shapely is the only python library on windows that is giving me this trouble in matlab -- i can use openCV, numpy, scipy, scikit's, mosek, picos, PIL, future, all fine. it's also the only library for which i need gohlke's installer. so i think there is a connection.

when i py.importlib.import_module('shapely') in matlab, i get this:

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]

a popup also comes up:

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

googling this, i get https://msdn.microsoft.com/en-us/library/ms235560(v=vs.90).aspx

Visual Studio 2008. An application has made an attempt to load the C runtime library without using a manifest. This is an unsupported way to load Visual C++ DLLs. You need to modify your application to build with a manifest. Applications must use a manifest to load the C runtime library. For more information, see Visual C++ Libraries as Shared Side-by-Side Assemblies and Manifest Generation in Visual Studio. ... To correct this error Rebuild your application to include a manifest. Building an application with Visual Studio automatically puts the manifest into the resulting .exe or .dll file. If you are building at the command line, use the mt.exe tool to add the manifest as a resource. Use resource ID 1 if you build an .exe, and resource ID 2 if you build a .dll. For more information, see How to: Embed a Manifest Inside a C/C++ Application."

this seems to relate to chris gohlke's comment here.

here are some more things i've tried.

in matlab:

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

x =

  Python NoneType with no properties.

    None

but same in python!

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

however, i can use shapely from python:

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

in 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']

in 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']

a search for geos_c.dll just shows shapely's directory in my python's site-packages.

a couple other SO answers suggest it's matlab putting its incompatible copy of msvcr*.dll somewhere on the path, and suspiciously:

>> 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']

so:

>> 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'>

yay! but as soon as i try to use it, i get exactly the same R6034 error popup

>> py.shapely.geometry.Point()

triggers the error popup > 10 times!

the following just does it once:

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

i also removed ...\AppData\Roaming\Python\Python27\site-packages from sys.path but that didn't change anything.

1

There are 1 answers

0
user1441998 On

we want to use matlab's msvcr, not hide it. in shapely, replacing CDLL(find_library('c')) with CDLL('C:\\Program Files\\MATLAB\\R2015a\\bin\\win64\\msvcr100.dll') makes it all work. this answer seems to be a method that would determine that path automatically, appropriate for wherever you are invoking shapely from.