在virtualenvwrapper环境中安装GDAL

15

我尝试在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 有点困难,因为它需要系统上已经安装了适当的 C++ 库。您还应该匹配版本(即,如果您的操作系统有 libgdal 1.9,则 pip install gdal==1.9 就是您的好帮手)。对于基于 Debian 的系统,您还需要安装 libgdal-dev。有时还会涉及到 CPPFLAGSLDFLAGS - dhke
Ubuntu 14.04,我的操作系统上的gdal版本是2.0.0,我尝试安装gdal==2.0.0但失败了! - GeoCom
2
您需要安装gdal-devgdal-bin包。运行 pip,并使用 CFLAGS="-I/usr/include/gdal" pip install gdal 命令可能也是必要的。 - dhke
谢谢。它对我有效! - GeoCom
3个回答

34

是的,在虚拟环境中安装GDAL确实有点麻烦。方便地是,我刚刚为我的导师实验室撰写了如何在虚拟环境中安装GDAL的文档!虽然我不够精通以确定您错误的确切原因,但我可以给您一些尝试修复它的方法。

首先,请确保您已在主机上安装了gdal(即不在虚拟环境中)。我只运行以下命令:

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

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

根据我的经验,在虚拟环境中获取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方面寻求帮助,我也有一些经验。


1
谢谢您的解释,特别是针对“无法找到版本满足要求pygdal==1.11.3(可用版本为:....)”错误的说明。 - ni8mr
这些指令确实帮助我使Kartograph.py运作起来,谢谢。 - colmjude
谢谢,伙计。我觉得提到版本号对我有用! - Salman Ghauri
谢谢,medley56,这很有帮助。 - Abhijay Ghildyal
1
现在的版本包含四个数字:pip install pygdal==1.11.3.3 - mitchus
这在我的Ubuntu 18.04上有效。稍后,我需要在我的20.04服务器上执行相同的操作。如果那里也能完美运行,我会告诉大家的。 - DarkCygnus

6
使用 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 High Sierra和Python3.6上工作了:pip install pygdal,然后from osgeo import gdal - Duccio A

2
在 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 - tupui

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接