IPython Windows 无法导入 geopandas

IPython on Windows fails to import geopandas

我得到一个

ImportError: DLL load failed: The specified procedure could not be found.

尝试在 python 3.6 中导入 geopandas 时出错。具体来说,我在使用 ipython 时遇到错误,但在使用 python 时却没有。此外,这会影响 Windows(Windows Server 2016 虚拟机)而不是 Linux。我之前发现了一些关于这个或非常相似的问题的 posts,但我拒绝他们的适用性,因为他们要么没有明确解决问题,要么将其与 pip 安装混为一谈。

例如,近两年前的 t 报告了类似的错误,但结论是“没关系,我安装了 geopandas”。

from just over a couple of years ago has an accepted answer despite the original poster commenting that it didn’t work for them! There’s a mention of a blog post 来自 Geoff Boeing,我之前看到它提供了一种工作方法,尽管该博客 post 提供了不止一种方法(conda 安装和更手动的步骤顺序)并且评论没有阐明对他们有用的东西。

近两年半前有 this post 将 conda 和 pip 安装方法混为一谈,但没有公认的答案。评论中有一个建议,对于评论者来说,这是 conda-forge 上 gdal 的问题。有一个答案再次引用了 Geoff Boeing 的博客post。这意味着通过 conda 安装 gdal 可能会有问题,如果有问题,则需要手动执行一系列步骤。我不相信这是我的问题。

我的问题特别发生在 Windows Server 2016 虚拟机上并且仅指定 conda-forge 通道时。此外,有针对性地,它只出现在 ipython(以及 Jupyter 笔记本)中,而不出现在 python 中,因此:

创建指定 conda 默认通道的环境,指定 python 3.6、ipython 和 geopandas:

conda create -n test36_defaults -c defaults python=3.6 ipython geopandas

激活该环境:

(test36_2) C:\Users\guy.maskall>conda activate test36_defaults

启动 ipython 内核并导入 geopandas:

(test36_defaults) C:\Users\guy.maskall>ipython
Python 3.6.12 |Anaconda, Inc.| (default, Sep  9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import geopandas as gpd

In [2]:
Do you really want to exit ([y]/n)?

导入没问题

同理,直接开火python解释器:

(test36_defaults) C:\Users\guy.maskall>python
Python 3.6.12 |Anaconda, Inc.| (default, Sep  9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import geopandas as gpd
>>>

进口的也不错。

现在创建另一个仅指定 conda-forge 的环境:

conda create -n test36_cforge -c conda-forge python=3.6 ipython geopandas

激活环境并再次加载ipython以导入geopandas:

(test36_cforge) C:\Users\guy.maskall>ipython
Python 3.6.11 (default, Aug  5 2020, 19:41:03) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import geopandas as gpd
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-a62d01c1d62e> in <module>()
----> 1 import geopandas as gpd

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\geopandas\__init__.py in <module>()
      5 from geopandas.array import points_from_xy  # noqa
      6
----> 7 from geopandas.io.file import _read_file as read_file  # noqa
      8 from geopandas.io.arrow import _read_parquet as read_parquet  # noqa
      9 from geopandas.io.arrow import _read_feather as read_feather  # noqa

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\geopandas\io\file.py in <module>()
      5 import pandas as pd
      6
----> 7 import fiona
      8 import pyproj
      9 from shapely.geometry import mapping

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__init__.py in <module>()
     84 import fiona._loading
     85 with fiona._loading.add_gdal_dll_directories():
---> 86     from fiona.collection import BytesCollection, Collection
     87     from fiona.drvsupport import supported_drivers
     88     from fiona.env import ensure_env_with_credentials, Env

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\collection.py in <module>()
      9 with fiona._loading.add_gdal_dll_directories():
     10     from fiona import compat, vfs
---> 11     from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
     12     from fiona.ogrext import Session, WritingSession
     13     from fiona.ogrext import buffer_to_virtual_file, remove_virtual_file, GEOMETRY_TYPES

ImportError: DLL load failed: The specified procedure could not be found.

In [2]:
Do you really want to exit ([y]/n)?

所以在 ipython 中失败了。现在尝试直接使用 python:

(test36_cforge) C:\Users\guy.maskall>python
Python 3.6.11 (default, Aug  5 2020, 19:41:03) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import geopandas as gpd
>>>

有效。

above SO posts 中的一个答案突出显示了 GDAL、Fiona、pyproj、rtree 和 shapely。 比较上面两个环境之间 conda list 的输出,我得到了完全相同的版本。除了 rtree 之外,所有的甚至都具有相同的构建。所以我有:

gdal                      3.1.4            py36h214b664_0    conda-forge  
fiona                     1.8.17           py36hdef4c2b_1 conda-forge  
pyproj                    2.6.1.post1      py36hfcef96e_3 conda-forge  
shapely                   1.7.1            py36h6dc46f0_1 conda-forge

rtree                     0.9.4            py36h9a6d676_1    conda-forge

(与不起作用的环境相关)vs

rtree                     0.9.4            py36h21ff451_1

(在有效的环境中)。

让我很困惑的是,尽管有一些 posts 暗示了 gdal,并且我上面的错误输出暗示了 fiona loading gdal(我认为),这两种环境似乎具有完全相同的版本和构建的 gdal 和fiona,错误仅发生在 ipython 内核中,而不发生在 python.

能够轻松复制环境对我来说很重要,因此首选 conda。除了简单地加载 geopandas 之外,我还有其他要求。比如我用的datacube,推荐python3.6。我以前 运行 是 python 的更高版本,我想获得 python 3.6 环境和 运行 这样我就可以看看这是否解决了另一个问题有。我想缩小导致此错误的原因,因为我希望能够轻松编辑我的 conda env yaml 以尝试不同的环境,并且我可能需要指定多个通道,因为一个嫌疑人(另一个问题)是 prompt-toolkit ,似乎只能在 esrf-bcu 频道的 3 以下版本中使用,但那是另一回事了。

所以谁能帮忙指点

python 版本有一个小差异(3.6.11 失败,3.6.12 成功),这是我可以探索的东西,但这仍然留下我的问题,为什么 ipython 对比 python.

其他新闻,我讨厌Windows。

正在导入 fiona 和 DLL 路径

更新 2020/11/02 由于错误似乎集中在 fiona 上,因此我将其归结为这一点。 在 anaconda 提示 shell 中,我 运行 set PYTHONVERBOSE=1,然后加载 ipython 并尝试导入 fiona:

In [1]: import fiona
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\__init__.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__init__.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\__init__.cpython-36.pyc'
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\_loading.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\_loading.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\_loading.cpython-36.pyc'
import 'fiona._loading' # <_frozen_importlib_external.SourceFileLoader object at 0x0000020344DC0C88>
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\__pycache__\socket.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\socket.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\__pycache__\socket.cpython-36.pyc'
# extension module '_socket' loaded from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\DLLs\_socket.pyd'
# extension module '_socket' executed from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\DLLs\_socket.pyd'
import '_socket' # <_frozen_importlib_external.ExtensionFileLoader object at 0x0000020344DCED30>
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\__pycache__\selectors.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\selectors.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\__pycache__\selectors.cpython-36.pyc'
import 'selectors' # <_frozen_importlib_external.SourceFileLoader object at 0x0000020344DD9358>
import 'socket' # <_frozen_importlib_external.SourceFileLoader object at 0x0000020344DCE198>
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\collection.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\collection.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\collection.cpython-36.pyc'
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\compat.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\compat.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\compat.cpython-36.pyc'
import 'fiona.compat' # <_frozen_importlib_external.SourceFileLoader object at 0x0000020344DE76D8>
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\vfs.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\vfs.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\vfs.cpython-36.pyc'
import 'fiona.vfs' # <_frozen_importlib_external.SourceFileLoader object at 0x0000020344DE7780>
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-4c762272c079> in <module>()
----> 1 import fiona

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _load_unlocked(spec)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap_external.py in exec_module(self, module)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__init__.py in <module>()
     84 import fiona._loading
     85 with fiona._loading.add_gdal_dll_directories():
---> 86     from fiona.collection import BytesCollection, Collection
     87     from fiona.drvsupport import supported_drivers
     88     from fiona.env import ensure_env_with_credentials, Env

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _load_unlocked(spec)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap_external.py in exec_module(self, module)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\collection.py in <module>()
      9 with fiona._loading.add_gdal_dll_directories():
     10     from fiona import compat, vfs
---> 11     from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
     12     from fiona.ogrext import Session, WritingSession
     13     from fiona.ogrext import buffer_to_virtual_file, remove_virtual_file, GEOMETRY_TYPES

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _load_unlocked(spec)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in module_from_spec(spec)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap_external.py in create_module(self, spec)

C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

ImportError: DLL load failed: The specified procedure could not be found.

但是然后退出 ipython 并直接启动 python 并尝试导入 fiona,我得到了更多的输出(节省了你所有的输出,但专注于 ipython 似乎摆脱困境):

...

import 'fiona.compat' # <_frozen_importlib_external.SourceFileLoader object at 0x00000289AC978470>
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\vfs.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\vfs.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\vfs.cpython-36.pyc'
import 'fiona.vfs' # <_frozen_importlib_external.SourceFileLoader object at 0x00000289AC997550>
# extension module 'fiona.ogrext' loaded from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\ogrext.cp36-win_amd64.pyd'
# extension module 'fiona._geometry' loaded from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\_geometry.cp36-win_amd64.pyd'
# extension module 'fiona._err' loaded from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\_err.cp36-win_amd64.pyd'
# extension module 'fiona._err' executed from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\_err.cp36-win_amd64.pyd'
import 'fiona._err' # <_frozen_importlib_external.ExtensionFileLoader object at 0x00000289AC9EE7F0>
# C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\errors.cpython-36.pyc matches C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\errors.py
# code object from 'C:\Users\guy.maskall\anaconda3\envs\test36_cforge\lib\site-packages\fiona\__pycache__\errors.cpython-36.pyc'
import 'fiona.errors' # <_frozen_importlib_external.SourceFileLoader object at 0x00000289AC9EEFD0>

...

这是在深入我真的不熟悉的胆量!但似乎我可以放心地说几句:

但我又回到了需要问为什么这个函数,或查找 DLL,与 python 相比,在 ipython 中的行为不同?

此外,我已经确认我在 Windows 10 Pro 2004 版上看到了同样的行为。

通过从 3.6.11 指定 python 3.6.12 即可解决特定的导入问题,因此:

conda create -n test36_12_cforge -c conda-forge python=3.6.12 ipython geopandas
(test36_12_cforge) C:\Users\guy.maskall>ipython
Python 3.6.12 |Anaconda, Inc.| (default, Sep  9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import geopandas as gpd

In [2]:
Do you really want to exit ([y]/n)?

(test36_12_cforge) C:\Users\guy.maskall>ipython
Python 3.6.12 |Anaconda, Inc.| (default, Sep  9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import fiona

In [2]:
Do you really want to exit ([y]/n)?

我不愿意接受我自己的回答。对于啤酒,如果我们见过面,谁能解释为什么会有这种行为差异,具体取决于进口是从 ipython 还是从 python 完成的?