21

I used

conda install gdal

to install the GDAL packages. But I had the following error when importing the packages.

>>> 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

What should I do to have GDAL imported in Python?

das-g
  • 9,718
  • 4
  • 38
  • 80
DQ_happy
  • 505
  • 2
  • 6
  • 20
  • I tried this but it turned out does not work....http://stackoverflow.com/questions/28848270/import-matplotlib-pyplot-gives-importerror-dlopen-library-not-loaded-libpng1/31892421#31892421 – DQ_happy Nov 06 '15 at 20:11
  • It works.. Many thanks to the author http://stackoverflow.com/questions/28848270/import-matplotlib-pyplot-gives-importerror-dlopen-library-not-loaded-libpng1/31892421#31892421 – DQ_happy Nov 06 '15 at 20:13
  • 3
    This is not solving the issue for me.:( – K.-Michael Aye Mar 17 '16 at 18:58

9 Answers9

21

For windows users (as of December 2015):

conda install gdal
conda upgrade numpy

Installing gdal will downgrade numpy, so then upgrade it back up. I recently had occasion to use windows for a change and I was pleasantly surprised that gdal "works" easily now.

Windows+python+gis people worldwide should be celebrating this. (that gdal-python goes in easily on windows...not that windows is one step closer to linux ;) )

user1269942
  • 3,772
  • 23
  • 33
  • What about Linux users? – zabop Dec 08 '21 at 09:55
  • @zabop first "sudo apt install gdal-bin" then "sudo apt install libgdal-dev". At this point gdal is on your system. then within a venv, pip install gdal. You may need to add a gis repo for gdal. I used to use the apt install python-gdal but not anymore. – user1269942 Dec 08 '21 at 15:56
15

I just made the mistake of executing the previously proposed command in the base environment of Conda:

conda install -c conda-forge gdal

This took ages to "solve environment" and, in the end, found numerous conflicts which halted the installation.

Given that, I instead created a separate environment with:

conda create -n gdal python=3.8

And activated it with:

conda activate gdal

And then executed the first command (as well as all others listed in the documentation). This worked fast and without any errors.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Christopholis
  • 159
  • 1
  • 2
  • Welcome. I appreciate you acknowledging what didn't work about the previous, accepted answer before introducing your own. That's especially valuable on these old posts, where it's useful to explain why your approach is relevant when there's already an accepted answer. Thank you. – Jeremy Caney Jun 02 '20 at 20:22
  • Thank you! This solved the problem I'd spent hours trying to fix. Just to note, the first time I tried this, I had to restart shell for some reason I don't quite remember. After restarting the shell, everything worked as intended. – thatsciencegal Mar 08 '21 at 17:32
  • Thanks! Totally saved my day with this answer. – entiat May 20 '21 at 20:03
6

you can also use the channel conda-forge

conda install -c conda-forge gdal

as suggested on the anaconda website.

andschar
  • 3,504
  • 2
  • 27
  • 35
2

I used

conda install -c conda-forge gdal

On a Fedora 30 machine and it lead me down a path of library conflict hell!

conda install gdal

worked on my first try

  • Both of these solutions worked for me. Note though that they would not work in an existing environment I had but would not explain why. It would just act like gdal doesn't exist. So, I had to create a separate environment just for GDAL, then it worked fine! – Akaisteph7 Jun 29 '22 at 18:28
2

The following works reliably for me 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 
mmann1123
  • 5,031
  • 7
  • 41
  • 49
2

This works for me

> CONDA_SUBDIR=osx-64 conda create -n my_env python=3.7
> conda activate my_env
> conda env config vars set CONDA_SUBDIR=osx-64
> conda install gdal
Now use,
> python3
> from osgeo import gdal
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 29 '22 at 06:43
0

I had the same problem, and after several days trying different solutions, I found the problem in conflict between Anaconda version and python version! if you have both Python and Anaconda on your system, then uninstall the python and use anaconda prompt to run this: pip install gdal

0

This worked for me . Hope it is useful to someone . I am using Ubuntu 20.04: It install osgeo also with gdal

Step 1 : conda install -c conda-forge gdal

If there is still some error then

Step 2 : conda config --set channel_priority strict”

Step 3 : conda update gdal

Aviator
  • 421
  • 1
  • 8
  • 15
0

For existing conda-env, I did do like below on 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
shinya
  • 21
  • 2