问题描述
利用Python进行绘图,在导入xarray库(包括cartopy、matplotlib、Eof等)中突然报错
ImportError: DLL load failed while importing lib: 找不到指定的程序。
折腾了两天,试过网上分享的各种方法:pip,conda重装各种库、新建虚拟环境、重装anaconda与spyder等方法都无法解决问题。
奇怪的是:该报错在重新进入spyder后时有时无。
解决方案:
通过排除法发现:更改import的运行顺序可以在一定程度上解决问题
示例如下:
1.报错:进入新控制台时,先运行如下的代码,后续就会一直出现DLL load failed错误
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from eofs.standard import Eof import pandas as pd import xarray as xr from scipy.interpolate import Rbf from matplotlib import rcParams from matplotlib.path import Path from matplotlib.patches import PathPatch import cartopy.crs as ccrs import shapefile from collections import Iterable import geopandas as gpd from shapely.geometry import Point as ShapelyPoint from shapely.geometry import Polygon as ShapelyPolygon from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter import cartopy.feature as cfeature
2.关掉原先的控制台,新建一个
3.先运行如下代码,不会出现DLL load failed错误,并且在该控制台下后续import其他库也不会报错
from matplotlib.path import Path
from matplotlib import rcParams
from matplotlib.patches import PathPatch
import shapefile
from collections import Iterable
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import cartopy.crs as ccrs
import geopandas as gpd
from shapely.geometry import Point as ShapelyPoint
from shapely.geometry import Polygon as ShapelyPolygon
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import cartopy.feature as cfeature
成功解决问题!