【问题标题】:Install gdal using conda?使用 conda 安装 gdal?
【发布时间】:2016-02-08 01:53:28
【问题描述】:

我用过

conda install gdal

安装 GDAL 软件包。但是我在导入包时遇到了以下错误。

>>> from osgeo import gdal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 21, in <module>
    _gdal = swig_import_helper()
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 17, in swig_import_helper
    _mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: dlopen(/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so, 2): Library not loaded: libgdal.20.dylib
  Referenced from: /Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so
  Reason: image not found
>>> from osgeo import ogr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 21, in <module>
    _gdal = swig_import_helper()
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 17, in swig_import_helper
    _mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: dlopen(/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so, 2): Library not loaded: libgdal.20.dylib
  Referenced from: /Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so
  Reason: image not found

如何在 Python 中导入 GDAL?

【问题讨论】:

标签: python gdal


【解决方案1】:

对于 Windows 用户(截至 2015 年 12 月):

conda install gdal
conda upgrade numpy

安装 gdal 会降级 numpy,所以然后升级它。 我最近有机会使用 windows 进行更改,我很惊喜 gdal 现在可以轻松“工作”。

Windows+python+gis 全世界的人们都应该庆祝这一点。 (gdal-python 很容易在 Windows 上运行……并不是说 Windows 离 linux 更近了一步;))

【讨论】:

  • Linux 用户呢?
  • @zabop 首先是“sudo apt install gdal-bin”,然后是“sudo apt install libgdal-dev”。此时 gdal 在您的系统上。然后在 venv 中,pip install gdal。您可能需要为 gdal 添加一个 gis 存储库。我曾经使用 apt install python-gdal 但现在不再使用了。
【解决方案2】:

我刚才在Conda的基础环境中执行之前提出的命令出错了:

conda install -c conda-forge gdal

这需要很长时间才能“解决环境问题”,最后发现许多冲突导致安装停止。

鉴于此,我创建了一个单独的环境:

conda create -n gdal python=3.8

并通过以下方式激活它:

conda activate gdal

然后然后执行了第一个命令(以及all others listed in the documentation)。这工作很快,没有任何错误。

【讨论】:

  • 欢迎。感谢您在介绍自己的答案之前承认先前已接受的答案的错误之处。这在这些旧帖子中特别有价值,在这些帖子中解释为什么你的方法在已经有一个公认的答案时是相关的很有用。谢谢。
  • 谢谢!这解决了我花了几个小时试图解决的问题。请注意,我第一次尝试这个时,由于某种我不太记得的原因,我不得不重新启动 shell。重新启动 shell 后,一切都按预期工作。
  • 谢谢!这个答案完全节省了我的时间。
【解决方案3】:

您也可以使用频道 conda-forge

conda install -c conda-forge gdal

按照anaconda website 的建议。

【讨论】:

  • 这与 conda 官方频道相比如何?
【解决方案4】:

我用过

conda install -c conda-forge gdal

在 Fedora 30 机器上,它让我走上了图书馆冲突地狱的道路!

conda install gdal

第一次尝试就成功了

【讨论】:

    【解决方案5】:

    我也遇到了同样的问题,尝试了几天不同的解决方案,发现Anaconda版本和python版本冲突的问题!如果您的系统上同时安装了 Python 和 Anaconda,请卸载 python 并使用 anaconda 提示符运行: 点安装gdal

    【讨论】:

      【解决方案6】:

      以下内容对我来说是可靠的 Ubuntu 20.04:

      conda update conda    
      sudo apt-get install libgdal-dev gdal-bin
      
      export CPLUS_INCLUDE_PATH=/usr/include/gdal
      export C_INCLUDE_PATH=/usr/include/gdal
      
      # check gdal version with 
      gdal-config --version
      
      echo '
      name: raster-pipeline
      channels:
      - defaults
      dependencies:
      - python=3.7.*
      - cython
      - numpy
      - ipython
      - libspatialindex
      - libgdal=USE_GDAL_VERSION
      - gdal=USE_GDAL_VERSION
      - pip
      - pip:
        - numpy>=1.18.5
        - GDAL==USE_GDAL_VERSION
        - pyproj>=2.6.1.post1
        - rasterio>=1.1.5
      ' > raster_pipeline.yml
        
      conda env create -f raster_pipeline.yml -v 
       
      conda activate raster_pipeline 
      python -c "from osgeo import gdal"
      conda deactivate 
      

      【讨论】:

        【解决方案7】:

        这对我有用。希望它对某人有用。我正在使用 Ubuntu 20.04:它也使用 gdal 安装 osgeo

        第 1 步:conda install -c conda-forge gdal

        如果还有错误,那么

        第 2 步:conda config --set channel_priority strict”

        第 3 步:conda 更新 gdal

        【讨论】:

          【解决方案8】:

          对于现有的 conda-env,我在 Ubuntu 20.04 上做了如下操作:

          conda update conda    
          sudo apt-get install libgdal-dev gdal-bin
          
          $export CPLUS_INCLUDE_PATH=/usr/include/gdal
          $export C_INCLUDE_PATH=/usr/include/gdal
          
          $gdal-config --version
          gdal3.0.4
          
          $whereis pip
          /usr/local/anaconda3/bin/pip # anaconda's pip
          
          $/usr/local/anaconda3/bin/pip install setuptools==57.4.0
          $/usr/local/anaconda3/bin/pip install gdal==3.0.4
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-15
            • 2014-10-12
            • 2017-06-30
            • 1970-01-01
            • 2018-12-24
            • 1970-01-01
            相关资源
            最近更新 更多