You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Jupyter Notebook用Python导入.tif遇rasterio循环导入错误求解决

问题:导入本地TIFF文件时出现rasterio循环导入错误

我尝试在Jupyter Notebook中用以下Python代码导入.tif文件:

image = 'location/map_Western_Cape.tif' #use same location as above
geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))

image_Gauteng = 'location/map_Gauteng.tif' #use same location as above
geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))

但持续出现如下错误:

AttributeError                            Traceback (most recent call last)
Input In [11], in <cell line: 4>()
      1 # Load the .tif from local machine
      3 image = 'C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Western_Cape.tif' #use same location as above
----> 4 geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))
      6 image_Gauteng = 'C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Gauteng.tif' #use same location as above
      7 geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))

File ~\anaconda3\lib\site-packages\geemap\common.py:12559, in plot_raster(image, band, cmap, proj, figsize, open_kwargs, **kwargs)
  12557 try:
  12558     import pvxarray
> 12559     import rioxarray
  12560     import xarray
  12561 except ImportError:

File ~\anaconda3\lib\site-packages\rioxarray\__init__.py:6, in <module>
      3 __author__ = """rioxarray Contributors"""
      4 __email__ = "alansnow21@gmail.com"
----> 6 import rioxarray.raster_array  # noqa
      7 import rioxarray.raster_dataset  # noqa
      8 from rioxarray._io import open_rasterio  # noqa

File ~\anaconda3\lib\site-packages\rioxarray\raster_array.py:28, in <module>
     14 from typing import (
     15     Any,
     16     Dict,
   (...)
     24     Union,
     25 )
     27 import numpy as np
--> 28 import rasterio
     29 import rasterio.mask
     30 import rasterio.warp

File ~\anaconda3\lib\site-packages\rasterio\__init__.py:44, in <module>
     41 import warnings
     43 import rasterio._loading
--> 44 with rasterio._loading.add_gdal_dll_directories():
     45     from rasterio._show_versions import show_versions
     46     from rasterio._version import gdal_version, get_geos_version, get_proj_version

AttributeError: partially initialized module 'rasterio' has no attribute '_loading' (most likely due to a circular import)

我已尝试安装所有相关模块,但仍报错,恳请推荐可行的.tif文件导入方法。


可行的TIFF导入与可视化方案

方案1:修复rasterio环境问题

该错误多由rasterio版本冲突或安装不完整导致,按以下步骤彻底重装依赖:

  • 卸载现有相关包:
pip uninstall -y rasterio rioxarray geemap
  • 用conda重新安装(conda能更好处理GDAL这类底层依赖):
conda install -c conda-forge rasterio rioxarray geemap
  • 重启Jupyter Notebook后修正原代码的变量错误(第二个plot_raster需使用image_Gauteng)再运行:
image = 'C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Western_Cape.tif'
geemap.plot_raster(image, cmap='terrain', figsize=(15, 10))

image_Gauteng = 'C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Gauteng.tif'
geemap.plot_raster(image_Gauteng, cmap='terrain', figsize=(15, 10))

方案2:改用rasterio+matplotlib直接读取可视化

如果geemap的依赖问题无法快速解决,直接用更基础的库实现需求:

import rasterio
from rasterio.plot import show
import matplotlib.pyplot as plt

# 读取并显示Western Cape的TIFF
with rasterio.open('C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Western_Cape.tif') as src:
    plt.figure(figsize=(15,10))
    show(src, cmap='terrain')
    plt.show()

# 读取并显示Gauteng的TIFF
with rasterio.open('C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Gauteng.tif') as src:
    plt.figure(figsize=(15,10))
    show(src, cmap='terrain')
    plt.show()

方案3:用xarray+rioxarray读取(适合地理数据后续处理)

如果需要对TIFF数据做进一步地理分析,用xarray加载更方便:

import rioxarray
import matplotlib.pyplot as plt

# 读取TIFF文件
ds_wc = rioxarray.open_rasterio('C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Western_Cape.tif')
ds_gt = rioxarray.open_rasterio('C:/Users/kiral/OneDrive - Stellenbosch University/BScHons Global Change/Guy/map_Gauteng.tif')

# 可视化
plt.figure(figsize=(15,10))
ds_wc.plot(cmap='terrain')
plt.show()

plt.figure(figsize=(15,10))
ds_gt.plot(cmap='terrain')
plt.show()

内容的提问来源于stack exchange,提问作者Kira Courtaillac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:20:42