1

I installed anaconda onto my system and tried numerous ways of installing GDAL each time on Spyder it gives me the same response: ImportError: No module named gdal

I wrote the program just as follows:

import gdal

Ran it then that error

Tried:

import osgeo import gdal

same issue.

So then I installed the Anaconda prompt. And it seemed to install anaconda when I typed in:

conda install -c conda-forge gdal

I went back to Spyder and got the same error.

Any ideas of why this is happening?

In the Anaconda prompt I wrote: gdalinfo --version And got back:

GDAL 2.2.4, released 2018/03/19

All seems up to date except when I run in Spyder

HISI
  • 4,557
  • 4
  • 35
  • 51
Ma_
  • 13
  • 1
  • 5

2 Answers2

3

It can be related to the environment variables used by Spyder, so you can try to execute spyder from the command line or compare and add the missing paths in Spyder.

  • To execute from command line:

    spyder
    
  • To see missing paths:

    Enter to a Python command line and run this:

    import sys
    sys.path
    

    Then in Spyder under menu Tools > PYTHONPATH manager option add the path

Note: You should import gdal like this from osgeo import gdal, import gdal is deprecated

Hope it helps!

References:

  • Thank you. Not sure I totally understand. In what command line do I enter it, the standard win-10 one or python command line? Also I tried both win-10 command line and python command line, each time I typed spyder it said 'spyder' is not recognized as an internal or external command. – Ma_ Apr 25 '18 at 19:56
  • I'm sorry, I assume you were using Linux, navigate to Spyder installation folder and then type spyder and press enter, this should starts the ide or try from python command line "from spyder.app import start" and "start.main()" – Julián Esteban Salomón Torres Apr 25 '18 at 20:09
  • When I am in the python command line I typed: from spyder.app import start. Tell me: ImportError: No module named spyder.app – Ma_ Apr 25 '18 at 20:15
  • How did you install Spyder? – Julián Esteban Salomón Torres Apr 25 '18 at 20:18
  • Just installed alongside Anaconda from their website. – Ma_ Apr 25 '18 at 20:21
  • Now when I run command prompt it tells me: 'gdalinfo' is not recognized as an internal or external command. I am lost on this. – Ma_ Apr 25 '18 at 21:18
  • I went to tools > PYTHONPATH manager > +Add path > C:\Program Files\GDAL – Ma_ Apr 25 '18 at 21:20
  • When I go to run it still says: ImportError: No module named gdal – Ma_ Apr 25 '18 at 21:20
  • And when I write, from osgeo import gdal, then it says ImportError: No module named osgeo – Ma_ Apr 25 '18 at 21:21
  • I tried to reproduce it, from Anaconda Prompt I executed `conda install gdall` `conda install spyder` and runned `spyder`, in Spyder imported gdal as mentioned (`from osgeo import gdal`) and works fine – Julián Esteban Salomón Torres Apr 25 '18 at 22:08
  • You can try to create a new conda environment to "start from zero" `conda create --name gdaltest`, activate the environment `conda activate gdaltest` and there install again gdal and spyder and execute spyder, to deactivate the environment use it `conda activate gdaltest` – Julián Esteban Salomón Torres Apr 25 '18 at 22:18
0

For me, it worked out by changing settings in the spyder interface:

Tools → Preferences → Python interpreter: C:/ProgramData/Anaconda2/envs/gdal/python.exe

After that change, spyder asked to run conda install ipykernel cloudpickle in the current environment. I did it and it worked. Another solution might be adding the same path (also in the spyder interface): Tools → PYTHONPATH manager.

anothernode
  • 5,100
  • 13
  • 43
  • 62