当前位置:   article > 正文

【已解决】OSError: could not find or load spatialindex_c-64.dll

【已解决】OSError: could not find or load spatialindex_c-64.dll

【已解决】OSError: could not find or load spatialindex_c-64.dll

我通过minconda安装spyder5.0后,每次启动都用报错
spyder启动时报错
虽说不影响正常使用,但是每次跳出来提醒你“你有错”还是很不爽的,于是我想了办法搞定了它,接下来我先介绍方法,在讲明原理。

办法
  1. 在python安装路径里搜索spatialindex_c-64.dll(为什么后面会讲到)
    搜索结果
  2. 然后点击文件所在位置,你会发现有两个dll文件应该是相辅相成的,一起复制(不要剪切)。
    有两个文件
  3. 接下来找到python第三方库安装路径,我的是D:\ProgramData\Miniconda3\Lib\site-packages,如果你是原生python你可以在python安装路径里搜索site-packages文件夹,在其中找到rtree的文件夹,我的如下图所示:
    rtree安装包
    rtree安装包内部
  4. 在rtree文件夹下粘贴ctrl + v,然后就可以随便使用spyder了。
原理

通过解析rtree源码可以得到上面的解决办法。
首先引用rtree,得到如下报错:

>>> import rtree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\ProgramData\Miniconda3\lib\site-packages\rtree\__init__.py", line 9, in <module>
    from .index import Rtree, Index  # noqa
  File "D:\ProgramData\Miniconda3\lib\site-packages\rtree\index.py", line 6, in <module>
    from . import core
  File "D:\ProgramData\Miniconda3\lib\site-packages\rtree\core.py", line 75, in <module>
    rt = finder.load()
  File "D:\ProgramData\Miniconda3\lib\site-packages\rtree\finder.py", line 67, in load
    raise OSError("could not find or load {}".format(lib_name))
OSError: could not find or load spatialindex_c-64.dll
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

错误在倒数的那个报错代码上,生成了OSError,因为找不到spatialindex_c-64.dll,那么让python能找到就可以了,就是说要搞清楚

  File "D:\ProgramData\Miniconda3\lib\site-packages\rtree\finder.py", line 67, in load
    raise OSError("could not find or load {}".format(lib_name))
  • 1
  • 2

finder.py的运行逻辑,注意到其上层报错

 File "D:\ProgramData\Miniconda3\lib\site-packages\rtree\core.py", line 75, in <module>
    rt = finder.load()
  • 1
  • 2

因此着重看load()函数

"""
finder.py
------------

Locate `libspatialindex` shared library by any means necessary.
"""
import os
import sys
import ctypes
import platform
from ctypes.util import find_library

# the current working directory of this file
_cwd = os.path.abspath(os.path.expanduser(
    os.path.dirname(__file__)))

# generate a bunch of candidate locations where the
# libspatialindex shared library *might* be hanging out
_candidates = [
    os.environ.get('SPATIALINDEX_C_LIBRARY', None),
    os.path.join(_cwd, 'lib'),
    _cwd,
    '']


def load():
    """
    Load the `libspatialindex` shared library.

    Returns
    -----------
    rt : ctypes object
      Loaded shared library
    """
    if os.name == 'nt':
        # check the platform architecture
        if '64' in platform.architecture()[0]:
            arch = '64'
        else:
            arch = '32'
        lib_name = 'spatialindex_c-{}.dll'.format(arch)

        # add search paths for conda installs
        if 'conda' in sys.version:
            _candidates.append(
                os.path.join(sys.prefix, "Library", "bin"))

        # get the current PATH
        oldenv = os.environ.get('PATH', '').strip().rstrip(';')
        # run through our list of candidate locations
        for path in _candidates:  # 从这里开始就是遍历路径,查找dll
            if not path or not os.path.exists(path):
                continue
            # temporarily add the path to the PATH environment variable
            # so Windows can find additional DLL dependencies.
            os.environ['PATH'] = ';'.join([path, oldenv])
            try:
                rt = ctypes.cdll.LoadLibrary(os.path.join(path, lib_name))
                if rt is not None:
                    return rt
            except (WindowsError, OSError):
                pass
            except BaseException as E:
                print('rtree.finder unexpected error: {}'.format(str(E)))
            finally:
                os.environ['PATH'] = oldenv
        raise OSError("could not find or load {}".format(lib_name))

    elif os.name == 'posix':

        # posix includes both mac and linux
        # use the extension for the specific platform
        if platform.system() == 'Darwin':
            # macos shared libraries are `.dylib`
            lib_name = "libspatialindex_c.dylib"
        else:
            # linux shared libraries are `.so`
            lib_name = 'libspatialindex_c.so'

        # get the starting working directory
        cwd = os.getcwd()
        for cand in _candidates:
            if cand is None:
                continue
            elif os.path.isdir(cand):
                # if our candidate is a directory use best guess
                path = cand
                target = os.path.join(cand, lib_name)
            elif os.path.isfile(cand):
                # if candidate is just a file use that
                path = os.path.split(cand)[0]
                target = cand
            else:
                continue

            if not os.path.exists(target):
                continue

            try:
                # move to the location we're checking
                os.chdir(path)
                # try loading the target file candidate
                rt = ctypes.cdll.LoadLibrary(target)
                if rt is not None:
                    return rt
            except BaseException as E:
                print('rtree.finder ({}) unexpected error: {}'.format(
                    target, str(E)))
            finally:
                os.chdir(cwd)

    try:
        # try loading library using LD path search
        rt = ctypes.cdll.LoadLibrary(
            find_library('spatialindex_c'))
        if rt is not None:
            return rt
    except BaseException:
        pass

    raise OSError("Could not load libspatialindex_c library")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122

注意上述代码中的中文注释,是我加上的,这说明我们需要搞清楚_candidates里有哪些路径,将rtree要找的dll复制过去不就ok了!

# the current working directory of this file
_cwd = os.path.abspath(os.path.expanduser(
    os.path.dirname(__file__)))

# generate a bunch of candidate locations where the
# libspatialindex shared library *might* be hanging out
_candidates = [
    os.environ.get('SPATIALINDEX_C_LIBRARY', None),
    os.path.join(_cwd, 'lib'),
    _cwd,
    '']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

_cwd_candidates列表中,因此这里只找_cwd的位置就行了,观其注释与函数调用,应该是finder.py所在的目录,即rtree的安装目录,对我来说就是D:\ProgramData\Miniconda3\Lib\site-packages\rtree
这就是我解决办法的由来,只要将要找的dll文件复制到安装目录就能解决问题。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/686098
推荐阅读
相关标签
  

闽ICP备14008679号