3

I am trying to use the python package Rasterio for raster analysis within Jupyter Notebook.

However, when I run import rasterio I receive the following error:

ImportError                               Traceback (most recent call last)
<ipython-input-6-350e27267e59> in <module>
----> 1 import rasterio

~\Anaconda3\lib\site-packages\rasterio\__init__.py in <module>
     15 from pathlib import Path
     16 
---> 17 from rasterio._base import gdal_version
     18 from rasterio.drivers import driver_from_extension, is_blacklisted
     19 from rasterio.dtypes import (

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

I was unsure if rasterio was even installed, and so I opened Anaconda Prompt (Anaconda3) and ran: conda list rasterio

This produced the following:

# packages in environment at C:\Users\MyName\Anaconda3:
#
# Name                    Version                   Build  Channel
rasterio                  1.2.8                    pypi_0    pypi

And so it looks like rasterio is installed.

Why then am I unable to import this package into Jupyter Notebook?

I know that with some of these packages there are key dependencies, such as gdal, and so I checked to make sure gdal is correctly installed by running in Anaconda Prompt (Anaconda3): conda list gdal

This returned:

# packages in environment at C:\Users\MyName\Anaconda3:
#
# Name                    Version                   Build  Channel
gdal                      3.2.3                    pypi_0    pypi
libgdal                   2.3.3                h10f50ba_0

Which looks like the package is installed. And then running from osgeo import gdal in Jupyter Notebook imported the package successfully.

6
  • 2
    Your output suggests you used pip to install it-- I'd start a fresh environment and install packages via conda only. The conda-forge channel is great for gis/scientific packages. If I remember correctly, conda create -n new_environment_name -c conda-forge rasterio
    – mikewatt
    Commented Dec 1, 2021 at 0:09
  • Ok I tried running your suggestion: conda create -n new_environment_name -c conda-forge rasterio in Anaconda Prompt, and I see rasterio downloaded successfully. I saw Downloading and Extracting Packages all go to 100% and then Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate new_environment_name # # To deactivate an active environment, use # # $ conda deactivate with no error message. Still, import rasterio results in the same error message as before in Jupter Notebook. Commented Dec 1, 2021 at 3:45
  • You need to activate the environment using conda activate new_environment_name and then open jupyter notebook. Now use import rasterio Commented Dec 1, 2021 at 9:23
  • I just tried this, and while running conda activate new_environment_name in Anaconda Prompt let me access the environment fine, subsequently opening Jupyter Notebook and then running import rasterio yielded the following error (shortened because its too long to show here): ImportError...15 from pathlib import Path 16 ---> 17 from rasterio._base import gdal_version 18 from rasterio.drivers import driver_from_extension, is_blacklisted 19 from rasterio.dtypes import ( ImportError: DLL load failed: The specified module could not be found. Commented Dec 1, 2021 at 18:50
  • I'd rule out jupyter first; activate with conda activate new_environment_name, run python to get an interactive interpreter on the command line, then see if you can import rasterio from there
    – mikewatt
    Commented Dec 1, 2021 at 20:19

1 Answer 1

3

I was having the same issue when trying to import rasterio from a jupyter notebook on VSCode, using a properly installed and activated conda environment.

When I tried mikewatt 's solution on the comments above and imported rasterio on a interactive interpreter session on the terminal, it gave me this error instead:

>>> import rasterio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\VirtualEcology\.conda\envs\sepadem\lib\site-packages\rasterio\__init__.py", line 28, in <module>
    from rasterio._version import gdal_version, get_geos_version, get_proj_version
ImportError: DLL load failed while importing _version: The specified procedure could not be found.

I then tried to manually import gdal first using import osgeo, and after that using import rasterio worked with no errors on the terminal. First importing osgeo and then rasterio also solved the issue on my VScode jupyter notebook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.