在Anaconda的Jupyter中使用Geopandas导入Shapefile时出现错误
I've run into this exact issue before with Anaconda and geopandas—this DLL error almost always boils down to incompatible dependencies or a messy installation of Fiona (the library geopandas relies on for reading shapefiles). Let's walk through how to fix it step by step:
Step 1: Clean up existing installations
First, we need to remove any potentially conflicting versions of geopandas and Fiona. Mixed installs via conda and pip are usually the culprit here, so let's uninstall both completely:
# Uninstall via conda first conda uninstall -y geopandas fiona # Make sure pip doesn't leave any leftovers pip uninstall -y geopandas fiona
Step 2: Reinstall using conda-forge
The conda-forge channel maintains pre-built packages with proper dependency handling for geospatial libraries. This will avoid the DLL missing issue entirely:
conda install -c conda-forge geopandas
This command will automatically install Fiona along with all its required dependencies (like GDAL) in a way that works seamlessly with your Anaconda environment.
Step 3: If problems persist, use a fresh virtual environment
Sometimes old environments have hidden dependency conflicts that are hard to track down. Create a clean environment specifically for geospatial work:
# Create a new environment (Python 3.9 is highly compatible with current geopandas versions) conda create -n geo_workspace python=3.9 -c conda-forge conda activate geo_workspace # Install geopandas in the fresh environment conda install geopandas
Step 4: Verify the fix
Open your Jupyter notebook again and run your code (I added a small tweak to print only the first few rows for sanity):
import geopandas as gpd gdf = gpd.read_file('C:\\Downloads\\Temp\\shapefile\\regions.shp') print(gdf.head()) gdf.plot()
If you see the GeoDataFrame output and a plot renders without errors, you're all set!
Quick note:
Avoid mixing conda and pip for geospatial packages like geopandas, Fiona, or GDAL. Pip's versions often don't account for Anaconda's DLL structure, leading to exactly this kind of error. Stick to conda-forge for reliable, conflict-free installs.
内容的提问来源于stack exchange,提问作者Codak

