虽然位置已知,但python ctypes.CDLL不会加载库

时间:2017-03-27 16:56:08

标签: python ctypes importerror shapely

要运行我的一个脚本,我必须使用shapely python包,但它崩溃了,因为它无法加载libgeos库。它给我以下错误消息:

Traceback (most recent call last):
  File "download_nl.py", line 2, in <module>
    from download_sentinel import sentinel_available, sentinel_download,     download_orbits
  File "/nfs/home2/gmulder/radar_database/download_sentinel.py", line 14, in <module>
    from fastkml import kml
  File "/home/gmulder/python_packages/fastkml/__init__.py", line 30, in <module>
    from .kml import KML, Document, Folder, Placemark
  File "/home/gmulder/python_packages/fastkml/kml.py", line 37, in <module>
    from .geometry import Geometry
  File "/home/gmulder/python_packages/fastkml/geometry.py", line 23, in <module>
    from shapely.geometry import Point, LineString, Polygon
  File "/home/gmulder/python_packages/shapely/geometry/__init__.py", line 4, in <module>
    from .base import CAP_STYLE, JOIN_STYLE
  File "/home/gmulder/python_packages/shapely/geometry/base.py", line 9, in <module>
    from shapely.coords import CoordinateSequence
  File "/home/gmulder/python_packages/shapely/coords.py", line 8, in <module>
    from shapely.geos import lgeos
  File "/home/gmulder/python_packages/shapely/geos.py", line 73, in <module>
    _lgeos = load_dll('geos_c', fallbacks=['libgeos_c.so.1', 'libgeos_c.so'])
  File "/home/gmulder/python_packages/shapely/geos.py", line 68, in load_dll
    libname, fallbacks or []))
OSError: Could not find lib geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so'].

然而,奇怪的是,当我以交互方式运行python时,导入正常,我没有任何错误。要检查设置正确路径是否有问题,我还检查了源。原始代码如下,其中libname给出了库名。

def load_dll(libname, fallbacks=None, mode=DEFAULT_MODE):
    lib = find_library(libname)
    dll = None
    print(sys.version)
    dirname = '/hpc/sw/geos-3.5.0/lib'
    os.environ['PATH'] = dirname + os.pathsep + os.environ['PATH']
    if os.path.exists(os.path.join(dirname, 'libgeos_c.so.1')):
        print('library does exist!')
    os.chdir(dirname)
    if lib is not None:
    try:
        LOG.debug("Trying `CDLL(%s)`", lib)
        dll = CDLL(lib)
    except OSError:
        LOG.warn("Failed `CDLL(%s)`", lib)
        pass

    if not dll and fallbacks is not None:
        for name in fallbacks:
            try:
                LOG.debug("Trying `CDLL(%s)`", name)
                dll = CDLL(name)
            except OSError:
                # move on to the next fallback
                LOG.warn("Failed `CDLL(%s)`", name)
                pass

    print(dll)
    if dll:
        LOG.debug("Library path: %r", lib or name)
        LOG.debug("DLL: %r", dll)
        return dll
    else:
        # No shared library was loaded. Raise OSError.
        raise OSError(
            "Could not find lib {0} or load any of its variants    {1}.".format(
                libname, fallbacks or []))

为了检查代码是否可以找到所需的lib文件位置,我在第7行添加了一个额外的检查,确认库在那里。但是,当我尝试加载它时,即使我直接将完整路径提供给CDLL函数,也不会加载任何内容。 我还尝试通过运行python并以交互方式(而不是从python脚本)打开库来加载库,这很好。因此,我检查了python版本,因为我认为这可能会导致这个问题。但我的脚本和交互式会话的python版本是相同的。有人可以帮我弄这个吗?可能我错过了什么,但我不知道是什么导致了这个问题。

0 个答案:

没有答案