【问题标题】:Install GDAL in virtualenvwrapper environment在 virtualenvwrapper 环境中安装 GDAL
【发布时间】:2026-01-21 10:00:01
【问题描述】:

我尝试在 virtualenvwrapper 环境中安装 gdal (pip install gdal),但出现此错误:

  error: command 'gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for gdal
 Failed to build gdal

我也试过“pip install --no-install GDAL”但是没有选项--no-install

我该怎么办!?

【问题讨论】:

  • 这是在哪个操作系统上? pyGDAL 使用 virtualenvs 有点困难,因为它需要系统上已经安装了适当的 C++ 库。您还应该匹配版本(即,如果您的操作系统有 libgdal 1.9,pip install gdal==1.9 是您的朋友)。对于基于 Debian 的系统,您还需要安装 libgdal-dev。有时还有CPPFLAGSLDFLAGS
  • Ubuntu 14.04 以及我在 Os 上的 gdal 版本是 2.0.0,我尝试安装 gdal==2.0.0 但不起作用!
  • 您需要 gdal-devgdal-bin 软件包。可能还需要运行 pipCFLAGS="-I/usr/include/gdal" pip install gdal
  • 谢谢。它对我有用!

标签: python pip virtualenv gdal virtualenvwrapper


【解决方案1】:

是的,在 venv 中安装 GDAL 很麻烦。方便的是,我刚刚为我的顾问实验室编写了有关如何执行此操作的文档!虽然我不够精明,无法查明错误的确切原因,但我可以为您提供很多东西来尝试解决它。​​

首先,确保您在主机上安装了 gdal(即不在 venv 中)。我只是运行以下命令:

sudo apt-get install libgdal1i libgdal1-dev libgdal-dev

现在运行gdal-config --version 以获取apt-get 为您提供的版本。例如我得到1.11.3

现在,根据我的经验,在 venv 中获取 python 绑定的最简单方法是使用pygdal。诀窍是获得正确的版本!为此,请激活您的虚拟环境并运行

pip install pygdal==1.11.3

但是用你从gdal-config --version 得到的任何东西替换版本。注意:您可能会收到一条错误提示

Could not find a version that satisfies the requirement pygdal==1.11.3 (from versions: 1.8.1.0, 1.8.1.1, 1.8.1.2, 1.8.1.3, 1.9.2.0, 1.9.2.1, 1.9.2.3, 1.10.0.0, 1.10.0.1, 1.10.0.3, 1.10.1.0, 1.10.1.1, 1.10.1.3, 1.11.0.0, 1.11.0.1, 1.11.0.3, 1.11.1.0, 1.11.1.1, 1.11.1.3, 1.11.2.1, 1.11.2.3, 1.11.3.3, 1.11.4.3, 2.1.0.3) No matching distribution found for pygdal==1.11.3

如果发生这种情况,请再次运行 pip install,但使用仍然匹配的最高版本。例如在这种情况下,您将运行 pip install pygdal==1.11.3.3

pygdal安装成功后,应该可以调用了

>>> from osgeo import gdal

如果有任何问题,请告诉我,我会尽我所能调整我的指示。此外,如果您在 Proj.4、GEOS 或 Cartopy 方面需要帮助,我也有一些经验。

【讨论】:

  • 感谢您的解释。特别是对于“找不到满足要求的版本 pygdal==1.11.3 (from versions: ....)”错误。
  • 这些说明确实帮助我让 Kartograph.py 正常工作,谢谢
  • 干杯伙伴。我认为提到版本对我有用!
  • 谢谢,medley56 这很有帮助
  • 版本现在包含四个数字:pip install pygdal==1.11.3.3
【解决方案2】:

使用pygdal

pd@asghar:~$sudo apt-get install python3-gdal 

pd@asghar:~$ virtualenv -p python3 test
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/pd/test/bin/python3
Also creating executable in /home/pd/test/bin/python
Installing setuptools, pip, wheel...done.

pd@asghar:~$  gdal-config --version
2.1.3
pd@asghar:~$ test/bin/pip install pygdal==2.1.3
Collecting pygdal==2.1.3
  Could not find a version that satisfies the requirement pygdal==2.1.3 (from versions: 1.8.1.0, 1.8.1.1, 1.8.1.2, 1.8.1.3, 1.9.2.0, 1.9.2.1, 1.9.2.3, 1.10.0.0, 1.10.0.1, 1.10.0.3, 1.10.1.0, 1.10.1.1, 1.10.1.3, 1.11.0.0, 1.11.0.1, 1.11.0.3, 1.11.1.0, 1.11.1.1, 1.11.1.3, 1.11.2.1, 1.11.2.3, 1.11.3.3, 1.11.4.3, 1.11.5.3, 2.0.0.3, 2.0.1.3, 2.0.2.3, 2.0.3.3, 2.1.0.3, 2.1.1.3, 2.1.2.3, 2.1.3.3, 2.2.0.3)
No matching distribution found for pygdal==2.1.3
pd@asghar:~$ test/bin/pip install pygdal==2.1.3.3
Collecting pygdal==2.1.3.3
Collecting numpy>=1.0.0 (from pygdal==2.1.3.3)
  Using cached numpy-1.13.1-cp35-cp35m-manylinux1_x86_64.whl
Installing collected packages: numpy, pygdal
Successfully installed numpy-1.13.1 pygdal-2.1.3.3
pd@asghar:~$ source test/bin/activate
(test) pd@asghar:~$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import gdal
>>> 

【讨论】:

  • 它在 OSX HighSIerra 和 Python3.6 上为我工作:pip install pygdal 然后from osgeo import gdal
【解决方案3】:

在 MacOS 上,首先进行 brew install:

brew install gdal

然后进行 pip 安装:

pip install gdal

【讨论】:

  • 不,这不适用于 virtualenv。为此,您必须进行链接。所以:brew tap osgeo/osgeo4mac 然后是 brew install osgeo-gdal && brew install osgeo-gdal-python 最后是 brew link osgeo-gdal --force && brew link osgeo-gdal-python --force。然后你可以在任何 virtualenv 中pip install gdal