Import Error when Importing Geopandas

只愿长相守 提交于 2021-02-09 08:43:33

问题


When trying to import geopandas into my jupyter notebook I get an ImportError: DLL load failed. I have already run pip install geopandas from my terminal and get "Requirement already satisfied". I have also tried pip install --upgrade pip setuptools which hasnt worked either. Here is the full error report when trying to import geopandas:

ImportError                               Traceback (most recent call last)
<ipython-input-2-fc7d1d298f0c> in <module>()
----> 1 import geopandas

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\geopandas\__init__.py in <module>()
      2 from geopandas.geodataframe import GeoDataFrame
      3 
----> 4 from geopandas.io.file import read_file
      5 from geopandas.io.sql import read_postgis
      6 from geopandas.tools import sjoin

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\geopandas\io\file.py in <module>()
      1 import os
      2 
----> 3 import fiona
      4 import numpy as np
      5 

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\fiona\__init__.py in <module>()
     67 from six import string_types
     68 
---> 69 from fiona.collection import Collection, BytesCollection, vsi_path
     70 from fiona._drivers import driver_count, GDALEnv
     71 from fiona.drvsupport import supported_drivers

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\fiona\collection.py in <module>()
      7 
      8 from fiona import compat
----> 9 from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
     10 from fiona.ogrext import Session, WritingSession
     11 from fiona.ogrext import (

ImportError: DLL load failed: The operating system cannot run %1.

All help is appreciated.


回答1:


The reason for the error is nicely explained by Prof. Boeing in his blogpost:

It seems that pip installing geopandas works fine on Linux and Mac. However, several of its dependencies have C extensions that cause compilation failures with pip on Windows... The best bet on Windows is to install Python wheels when possible, because they contain pre-compiled extensions. The conda package manager that comes with Anaconda does this for packages available in its repository. Alternatively, Christoph Gohlke at the Laboratory for Fluorescence Dynamics at UC Irvine maintains a large library of Python wheels for Windows.

Solution: You have to uninstall geopandas and its dependencies manually and install the packages manually, that was the solution of this problem in my case. To do that:

  1. At first you have to uninstall any existing packages of OSGeo4W, GDAL, Fiona, pyproj, rtree, or shapely by writing conda uninstall <package name> (for example: conda uninstall fiona) in the command prompt.

  2. Download the wheels for GDAL, Fiona, pyproj, rtree, and shapely from Gohlke. Make sure you choose the wheel files that match your architecture (64-bit) and Python version (2.7 or 3.6).

  3. pip install the wheel files you downloaded according to the orders mentioned in step 2. Commands will be like: pip install GDAL-2.2.4-cp36-cp36m-win_amd64.whl.

  4. Add the new GDAL path (something like C:\Anaconda\Lib\site-packages\osgeo, you have find the location of osgeo in your computer) to the windows PATH environment variable. To know the procedure of adding a new PATH click here.




回答2:


First of all add conda channels to your settings (last channel has the highest priority).

conda config --add channels conda-forge
conda config --add channels defaults

Then try to create new environment using conda.

conda create -n test_python python=3.7 geopandas

In my case it was a problem with gdal, especially with latest release on conda-forge (previous one works fine). If geopandas installation in test environment fails you can try to install this release (win64) of gdal using conda.

conda install "downloaded file path"



回答3:


I encountered this recently when installing geopandas on my work machine (which I don't have administrator rights to add directories to PATH environment).

I realised the pip install processes created 2 folders for osgeo. I just copied the files with all the executables (*.exe) to the folder with the python scripts (*.py) and the imports work fine. (I'm guessing the python scripts assumed the other files are within the same directory)

You may have to restart jupyter after moving the files for it to register the changes.

e.g.: Copy the contents of Users\USERNAME\AppData\Roaming\Python\Lib\site-packages\osgeo to Users\USERNAME\AppData\Roaming\Python\Python27\site-packages\osgeo

Hope it helps.



来源:https://stackoverflow.com/questions/50490515/import-error-when-importing-geopandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!