简体   繁体   English

导入 Geopandas 时导入错误

[英]Import Error when Importing Geopandas

When trying to import geopandas into my jupyter notebook I get an ImportError: DLL load failed .尝试将 geopandas 导入我的 jupyter 笔记本时,出现ImportError: DLL load failed I have already run pip install geopandas from my terminal and get "Requirement already satisfied".我已经从我的终端运行pip install geopandas并得到“要求已经满足”。 I have also tried pip install --upgrade pip setuptools which hasnt worked either.我也试过pip install --upgrade pip setuptools也没有用。 Here is the full error report when trying to import geopandas:这是尝试导入 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.感谢所有帮助。

First of all add conda channels to your settings (last channel has the highest priority).首先将 conda 通道添加到您的设置中(最后一个通道具有最高优先级)。

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

Then try to create new environment using conda.然后尝试使用 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).就我而言,这是 gdal 的问题,尤其是 conda-forge 上的最新版本(前一个工作正常)。 If geopandas installation in test environment fails you can try to install this release (win64) of gdal using conda.如果在测试环境中安装 geopandas 失败,您可以尝试使用 conda 安装此版本 (win64) 的gdal

conda install "downloaded file path"

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. pip 安装 geopandas 似乎在 Linux 和 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.但是,它的几个依赖项具有 C 扩展,这会导致在 Windows 上使用 pip 编译失败...... Windows 上的最佳选择是尽可能安装 Python 轮子,因为它们包含预编译的扩展。 The conda package manager that comes with Anaconda does this for packages available in its repository. Anaconda 附带的 conda 包管理器对其存储库中可用的包执行此操作。 Alternatively, Christoph Gohlke at the Laboratory for Fluorescence Dynamics at UC Irvine maintains a large library of Python wheels for Windows.或者,加州大学欧文分校荧光动力学实验室的 Christoph Gohlke 为 Windows 维护了一个大型 Python 轮子库。

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.解决方案:您必须手动卸载 geopandas 及其依赖项并手动安装软件包,在我的情况下,这就是解决此问题的方法。 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.首先你必须卸载任何现有的 OSGeo4W、GDAL、Fiona、pyproj、rtree 或 shapely conda uninstall <package name> ,方法是在命令提示符下写conda uninstall <package name> (例如: conda uninstall fiona )。

  2. Download the wheels for GDAL, Fiona, pyproj, rtree, and shapely from Gohlke .Gohlke下载 GDAL、Fiona、pyproj、rtree 和shapely 的轮子 Make sure you choose the wheel files that match your architecture (64-bit) and Python version (2.7 or 3.6).确保选择与您的架构(64 位)和 Python 版本(2.7 或 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 . pip install 你下载的wheel文件,按照步骤2中的命令,命令如下: 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.将新的 GDAL 路径(类似于 C:\\Anaconda\\Lib\\site-packages\\osgeo,您已在计算机中找到 osgeo 的位置)添加到 windows PATH 环境变量中。 To know the procedure of adding a new PATH click here .要了解添加新 PATH 的过程,请单击此处

I encountered this recently when installing geopandas on my work machine (which I don't have administrator rights to add directories to PATH environment).我最近在我的工作机器上安装 geopandas 时遇到了这个问题(我没有将目录添加到 PATH 环境的管理员权限)。

I realised the pip install processes created 2 folders for osgeo.我意识到 pip 安装过程为 osgeo 创建了 2 个文件夹。 I just copied the files with all the executables (*.exe) to the folder with the python scripts (*.py) and the imports work fine.我只是将包含所有可执行文件 (*.exe) 的文件复制到带有 python 脚本 (*.py) 的文件夹中,并且导入工作正常。 (I'm guessing the python scripts assumed the other files are within the same directory) (我猜 python 脚本假设其他文件在同一目录中)

You may have to restart jupyter after moving the files for it to register the changes.您可能需要在移动文件后重新启动 jupyter 以注册更改。

eg: Copy the contents of Users\\USERNAME\\AppData\\Roaming\\Python\\Lib\\site-packages\\osgeo to Users\\USERNAME\\AppData\\Roaming\\Python\\Python27\\site-packages\\osgeo例如:将Users\\USERNAME\\AppData\\Roaming\\Python\\Lib\\site-packages\\osgeoUsers\\USERNAME\\AppData\\Roaming\\Python\\Python27\\site-packages\\osgeo

Hope it helps.希望能帮助到你。

We faced the same issue, it was solved for us by using Python 3.7.1 and geopandas 0.6.1:我们遇到了同样的问题,使用 Python 3.7.1 和 geopandas 0.6.1 为我们解决了这个问题:

conda create -n test_python python=3.7.1 geopandas=0.6.1

And to switch to the right environment in Jupyter we installed nb_conda (see How to choose your conda environment in Jupyter Notebook )为了在 Jupyter 中切换到正确的环境,我们安装了 nb_conda(请参阅如何在 Jupyter Notebook 中选择您的 conda 环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM