ImportError when importing PCRaster Python package

Some PCRaster users have reported that loading the PCRaster Python package on Windows does not work. When Python tries to import the package, it fails saying:

Traceback (most recent call last):
File "my_script.py", line 12, in
import PCRaster as pcr
File "C:\Program Files\PCRaster\Python\PCRaster\__init__.py", line 1, in
from operations import *
File "C:\Program Files\PCRaster\Python\PCRaster\operations.py", line 3, in
import PCRaster._PCRaster as _PCRaster
ImportError: DLL load failed: The specified procedure could not be found.

I will try to explain why this happens and what you can do to solve it.

Python tries to import the _PCRaster.pyd dll file which is part of the PCRaster Python package. Like most dlls, this dll depends on (specific versions of) other dlls. Python fails to import _PCRaster because there is an issue with at least one of these other dlls.

A PCRaster installation contains the correct version of all required dlls. One reason the import failure can happen is because other software is installed containing one or more of the dlls that _PCRaster.pyd depends on, but a different version. This is not a problem, unless Windows loads one of these other dlls instead of the ones shipped with PCRaster.

Which dlls get loaded during the execution of software on Windows is a complicated topic. See for example this page. Whatever the reason, to solve the import failure, you need to find out which conflicting dll(s) Windows tries to load. For that, you can use the Dependency Walker.

Open the PCRaster python extension (.pyd) with Dependency Walker and look at error messages in the bottom pane. Scroll through all the modules in the middle pane, looking for errors. Make sure Dependency Walker has the same environment (PATH) settings as you normally have when running your Python script that imports the PCRaster extension.

Verify that all dlls that get loaded by _PCRaster.pyd are loaded from the PCRaster installation directory, or one of the standard Windows directories. If any of the dlls get loaded from some other application’s directory, then that is most probably the cause of the import failure. To test this, rename the other dll(s) and restart the Dependency Walker. Not all issues reported by Dependency Walker are real errors. See for example the second question in the Dependency Walker FAQ.

When all issues with conflicting dlls are solved you can try to import the PCRaster Python package. The import failure should be solved now.