Install GDAL in Python

1. Installation steps

Method 1 is simpler, but there may be more problems. Method 2 is more intuitive and successful once.

1.1 Method 1

Step 1, enter in the terminal/command line:

pip install gdal

In case of error:

Traceback (most recent call last):
    File "/tmp/pip-install-h_n_kokk/gdal_0c5c253514974d76a20d2183c5b4798d/setup.py", line 188, in get_gdal_config
      return fetch_config(option, gdal_config=self.gdal_config)
    File "/tmp/pip-install-h_n_kokk/gdal_0c5c253514974d76a20d2183c5b4798d/setup.py", line 90, in fetch_config
      raise gdal_config_error(e)
  __main__.gdal_config_error: [Errno 2] No such file or directory: 'gdal-config': 'gdal-config'
  
  During handling of the above exception, another exception occurred:

Step 2, install libgdal-dev:

 sudo apt install libgdal-dev

Repeat step 1, if an error occurs:

extensions/gdal_wrap.cpp:48261:97: error: ‘GVOT_MIN_TARGET_HEIGHT_FROM_DEM’ was not declared in this scope
       SWIG_Python_SetConstant(d, "GVOT_MIN_TARGET_HEIGHT_FROM_DEM",SWIG_From_int(static_cast< int >(GVOT_MIN_TARGET_HEIGHT_FROM_DEM)));
                                                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    extensions/gdal_wrap.cpp:48262:100: error: ‘GVOT_MIN_TARGET_HEIGHT_FROM_GROUND’ was not declared in this scope
       SWIG_Python_SetConstant(d, "GVOT_MIN_TARGET_HEIGHT_FROM_GROUND",SWIG_From_int(static_cast< int >(GVOT_MIN_TARGET_HEIGHT_FROM_GROUND)));
                                                                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    extensions/gdal_wrap.cpp: At global scope:
    extensions/gdal_wrap.cpp:5383:15: warning: ‘MDArrayReadWriteCheckArguments’ defined but not used [-Wunused-variable]
     static CPLErr MDArrayReadWriteCheckArguments(GDALMDArrayHS* array,
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    extensions/gdal_wrap.cpp:5360:13: warning: ‘CheckNumericDataType’ defined but not used [-Wunused-variable]
     static bool CheckNumericDataType(GDALExtendedDataTypeHS* dt)
                 ^~~~~~~~~~~~~~~~~~~~
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/zwshi/PycharmProjects/LaserAltimetry/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rw1cca2p/gdal_71f0a1fbeb04448fa91e2007642cbb8d/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rw1cca2p/gdal_71f0a1fbeb04448fa91e2007642cbb8d/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-mcpr3nb_/install-record.txt --single-version-externally-managed --compile --install-headers /home/zwshi/PycharmProjects/LaserAltimetry/venv/include/site/python3.6/gdal Check the logs for full command output.

Step 3, install gdal's dependent library:

sudo apt-get install gdal-bin libgdal-dev python3-gdal

Then, repeat step 1 and you should be good to go.

1.2 Method 2

GDAL official website download, download address: https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
The current official website provides the following versions of the GDAL library, win32 represents 32-bit systems, amd64 represents 64-bit operating systems, and cp36 represents The Python you installed is version 3.6, and so on.
insert image description here

If the Python interpreter has been installed, then Windows is in cmd, cd to the folder where GDAL-3.4.3-cp38-cp38-win32.whl is located, and enter the command:

pip install GDAL-3.4.3-cp38-cp38-win32.whl

I installed Anaconda, in the startup program, select Anaconda prompt (myenv), then cd to the folder where GDAL-3.4.3-cp38-cp38-win32.whl is located, and then enter the command:

pip install GDAL-3.4.3-cp38-cp38-win32.whl

The following command will not work

conda install GDAL-3.4.3-cp38-cp38-win32.whl

2. Test

test:

from __future__ import print_function
from osgeo import gdal
print("GDAL's version is:" + gdal.__version__)
print(gdal)

output:

GDAL's version is:2.2.3
<module 'osgeo.gdal' from '/usr/lib/python3/dist-packages/osgeo/gdal.py'>

3. Reference

[1] ChatGPT. www.openai.com
[2] GDAL for python tutorial
[3] Install GDAL in Python (the simplest and most detailed graphic tutorial)
[4] Python configuration and installation of gdal library (under Windows)

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/129011091