【已解决】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

错误在倒数的那个报错代码上,生成了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))

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

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

因此着重看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")

注意上述代码中的中文注释,是我加上的,这说明我们需要搞清楚_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,
    '']

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

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值