我通过minconda安装spyder5.0后,每次启动都用报错
虽说不影响正常使用,但是每次跳出来提醒你“你有错”还是很不爽的,于是我想了办法搞定了它,接下来我先介绍方法,在讲明原理。
- 在python安装路径里搜索spatialindex_c-64.dll(为什么后面会讲到)
- 然后点击文件所在位置,你会发现有两个dll文件应该是相辅相成的,一起复制(不要剪切)。
- 接下来找到python第三方库安装路径,我的是D:ProgramDataMiniconda3Libsite-packages,如果你是原生python你可以在python安装路径里搜索site-packages文件夹,在其中找到rtree的文件夹,我的如下图所示:
- 在rtree文件夹下粘贴ctrl + v,然后就可以随便使用spyder了。
通过解析rtree源码可以得到上面的解决办法。
首先引用rtree,得到如下报错:
>>> import rtree Traceback (most recent call last): File "", line 1, in File "D:ProgramDataMiniconda3libsite-packagesrtree__init__.py", line 9, in from .index import Rtree, Index # noqa File "D:ProgramDataMiniconda3libsite-packagesrtreeindex.py", line 6, in from . import core File "D:ProgramDataMiniconda3libsite-packagesrtreecore.py", line 75, in rt = finder.load() File "D:ProgramDataMiniconda3libsite-packagesrtreefinder.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:ProgramDataMiniconda3libsite-packagesrtreefinder.py", line 67, in load
raise OSError("could not find or load {}".format(lib_name))
finder.py的运行逻辑,注意到其上层报错
File "D:ProgramDataMiniconda3libsite-packagesrtreecore.py", line 75, inrt = 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:ProgramDataMiniconda3Libsite-packagesrtree。
这就是我解决办法的由来,只要将要找的dll文件复制到安装目录就能解决问题。



