'Python ImportError: DLL load failed while importing

On Windows, I have an environment having Python==3.9.6' installed. I have a strange issue. Having two packages rasterio==1.2.6 and h5py==3.3.0 installed, either of these two packages are imported first, working well, but the second one is having the following error:

import rasterio
# Imported fine!

import h5py
Traceback (most recent call last):
  File "C:\Users\amin_\Anaconda3\envs\minusone\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-c9f0b8c65221>", line 1, in <module>
    import h5py
  File "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\amin_\Anaconda3\envs\minusone\lib\site-packages\h5py\__init__.py", line 33, in <module>
    from . import version
  File "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\amin_\Anaconda3\envs\minusone\lib\site-packages\h5py\version.py", line 15, in <module>
    from . import h5 as _h5
  File "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "h5py\h5.pyx", line 1, in init h5py.h5
  File "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed while importing defs: The specified procedure could not be found.

And :

import h5py
# Imported!

import rasterio

Traceback (most recent call last):
  File "C:\Users\amin_\Anaconda3\envs\minusone\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-350e27267e59>", line 1, in <module>
    import rasterio
  File "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\amin_\Anaconda3\envs\minusone\lib\site-packages\rasterio\__init__.py", line 9, in <module>
    from rasterio._base import gdal_version
  File "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed while importing _base: The specified procedure could not be found.



Solution 1:[1]

I faced a similar error with the rdkit package and PyDev and the solution was incredibly simple. In my case, import was failing because the package was using an environment variable to load the modules, but PyDev was not charging that variable to run the script. Just ensure that conda load the environtment variables before run your script.

It could be easily done checking the option 'Load conda env vars before run?' on Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alejandro López