Cannot import gdal in jupyter notebook

Abdul Haseeb Azizi abdulhaseeb.azizi786 at gmail.com
Wed Oct 12 03:51:11 EDT 2022


On Tuesday, October 11, 2022 at 10:29:34 PM UTC+2, MRAB wrote:
> On 2022-10-11 20:38, Thomas Passin wrote: 
> > On Windows, when I tried to install gdal using pip, it needed to compile 
> > part of it. I'm not set up with the Microsoft compiler and header 
> > files, so that failed. If you are on Windows, you will need to look for 
> > a binary wheel to install, or install and configure the compiler and 
> > header files. 
> >
> The PyPI page at https://pypi.org/project/GDAL/ has this: 
> 
> """ 
> Conda 
> GDAL can be quite complex to build and install, particularly on Windows 
> and MacOS. Pre built binaries are provided for the conda system: 
> 
> https://docs.conda.io/en/latest/ 
> 
> By the conda-forge project: 
> 
> https://conda-forge.org/
> """ 
> 
> > On 10/11/2022 11:53 AM, Abdul Haseeb Azizi wrote:
> >> Hi everyone, 
> >> I am new to python and I am trying to utilize this code "from osgeo import gdal". I installed python 3.10 and also I installed anaconda3 to solve this matter but I could not succeed. When I run that code I get the following error. 
> >> --------------------------------------------------------------------------- 
> >> ModuleNotFoundError Traceback (most recent call last) 
> >> Cell In [1], line 1 
> >> ----> 1 from osgeo import gdal 
> >> 
> >> ModuleNotFoundError: No module named 'osgeo' 
> >> 
> >> I also tried to create anaconda environment and enter the following codes: 
> >> conda crate --name pygdal 
> >> conda activate pygdal 
> >> conda install -c conda-forge gdal 
> >> 
> >> I don't what is the problem. Any help is appreciated to solve this matter. 
> >> Looking forward to hear from you. 
> >> 
> >> with regards 
> >> Abdul 
> >
Thanks a lot for your recommendations.
As I mentioned before I installed conda and also created conda environment using the following codes:
Step 1: Create conda environment

conda create --name venv

After creating the virtual environment, by default it will install some of the packages.
Step 2: Installation of gdal

conda install -c conda-forge gdal

Step 3: Installation of ipykernel

conda install -c anaconda ipykernel

Step 4: Add ipykernel and run jupyternotebook

python -m ipykernel install --user --name=venv
jupyter notebook


still I get the following error:

%pip install gdal

Requirement already satisfied: gdal in c:\users\abdul\anaconda3\lib\site-packages (3.4.1)
Note: you may need to restart the kernel to use updated packages.

from osgeo import gdal


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\osgeo\__init__.py:29, in swig_import_helper()
     28 try:
---> 29     return importlib.import_module(mname)
     30 except ImportError:

File ~\anaconda3\lib\importlib\__init__.py:127, in import_module(name, package)
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1030, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1007, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:986, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:666, in _load_unlocked(spec)

File <frozen importlib._bootstrap>:565, in module_from_spec(spec)

File <frozen importlib._bootstrap_external>:1173, in create_module(self, spec)

File <frozen importlib._bootstrap>:228, in _call_with_frames_removed(f, *args, **kwds)

ImportError: DLL load failed while importing _gdal: The specified module could not be found.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 from osgeo import gdal

File ~\anaconda3\lib\site-packages\osgeo\__init__.py:45, in <module>
     41                 raise ImportError(traceback_string + '\n' + msg)
     42         return importlib.import_module('_gdal')
---> 45 _gdal = swig_import_helper()
     46 del swig_import_helper
     48 __version__ = _gdal.__version__ = _gdal.VersionInfo("RELEASE_NAME")

File ~\anaconda3\lib\site-packages\osgeo\__init__.py:41, in swig_import_helper()
     39         import traceback
     40         traceback_string = ''.join(traceback.format_exception(*sys.exc_info()))
---> 41         raise ImportError(traceback_string + '\n' + msg)
     42 return importlib.import_module('_gdal')

ImportError: Traceback (most recent call last):
  File "C:\Users\abdul\anaconda3\lib\site-packages\osgeo\__init__.py", line 29, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\abdul\anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1173, in create_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
ImportError: DLL load failed while importing _gdal: The specified module could not be found.

On Windows, with Python >= 3.8, DLLs are no longer imported from the PATH.
If gdalXXX.dll is in the PATH, then set the USE_PATH_FOR_GDAL_PYTHON=YES environment variable
to feed the PATH into os.add_dll_directory().

I hope you could look at the errors and guide me to solve this problem.

with regards
Abdul


More information about the Python-list mailing list