Compile NumPy with MKL on Windows - DLL load failed

cubanpit

I've been trying to compile NumPy from source on Windows 10, with MSVC compiler and Intel MKL. I am running Windows 10.0.18363 with Microsoft Visual Studio 2019 (16.8.4) and Intel MKL 2017.8.275.

I managed to reproduce the issue with a minimal setup, using latest Python and NumPy.

  1. Download latest Python (3.9.1) and latest NumPy (1.20.1) source.

  2. Open a VS command prompt, unpack Python source, build with PCbuild\build.bat

  3. Run mklvars.bat intel64 to get the right environment variables set.

  4. Add the Intel compilers (needed for ifort) to PATH:

    set PATH=C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\bin\intel64;%PATH%
    
  5. Create a virtual env, copy a few files from the Python build and activate the virtual env:

    copy Python\PCbuild\amd64\python39.dll venv\Scripts
    copy Python\PC\pyconfig.h venv\Include
    
  6. Build NumPy from source and install: pip install . -v

  7. Try to import NumPy: python -c "import numpy"

Error message

Traceback (most recent call last):
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
    from . import multiarray
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
    from . import overrides
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\__init__.py", line 145, in <module>
    from . import core
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "C:\cygwin\home\user\numpy_clean_env\venv\Scripts\python.exe"
  * The NumPy version is: "1.20.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.

Other information

The MKL libraries are picked up during compilation since it returns:

FOUND:
        libraries = ['mkl_rt']
        library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl\\lib\\intel64']
        define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
        include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl\\lib']

I tried to analyze the DLL resolution with Dependencies (the newer version of Dependency Walker) but it seems that the MKL DLL loads fine. There are some DLLs that appear as not correctly loaded, but as far as I understand it is caused by the inspection software limit with Windows API sets, not by actual problems with this DLLs, so I think the system is correctly setup. Here is a list of the unique DLLs that cause a NOT_FOUND in Dependencies when analyzing _multiarray_umath.pyd:

EMCLIENT.dll (NOT_FOUND) :
HvsiFileTrust.dll (NOT_FOUND) :
UpdateAPI.dll (NOT_FOUND) :
api-ms-win-core-comm-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-psapi-ansi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-psapi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-stringansi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-versionansi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-versionansi-l1-1-1.dll (NOT_FOUND) :
api-ms-win-core-winrt-string-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-wow64-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-wow64-l1-1-1.dll (NOT_FOUND) :
api-ms-win-core-xstate-l2-1-0.dll (NOT_FOUND) :
api-ms-win-core-xstate-l2-1-1.dll (NOT_FOUND) :
api-ms-win-coreui-secruntime-l1-1-0.dll (NOT_FOUND) :
ext-ms-mf-pal-l2-1-0.dll (NOT_FOUND) :
ext-ms-onecore-appmodel-emclient-l1-1-0.dll (NOT_FOUND) :
ext-ms-onecore-defaultdiscovery-l1-1-0.dll (NOT_FOUND) :
ext-ms-onecore-orientation-l1-1-0.dll (NOT_FOUND) :
ext-ms-onecore-shellchromeapi-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-appmodel-deployment-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-appmodel-usercontext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-appmodel-viewscalefactor-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-audiocore-pal-l1-2-0.dll (NOT_FOUND) :
ext-ms-win-com-suspendresiliency-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-core-winrt-remote-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-dwmapi-ext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-dxcore-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-networking-wlanstorage-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-1.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-2.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-3.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-4.dll (NOT_FOUND) :
ext-ms-win-ras-tapi32-l1-1-1.dll (NOT_FOUND) :
ext-ms-win-ro-typeresolution-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-rtcore-minuser-input-l1-1-1.dll (NOT_FOUND) :
ext-ms-win-rtcore-minuser-private-ext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-security-capauthz-ext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-security-chambers-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell-knownfolderext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell-shell32-l1-2-0.dll (NOT_FOUND) :
ext-ms-win-shell-shlwapi-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell-tabbedtitlebar-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell32-shellcom-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell32-shellfolders-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-ui-viewmanagement-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-uiacore-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-wer-xbox-l1-1-0.dll (NOT_FOUND) :
ext-ms-win32-subsystem-query-l1-1-0.dll (NOT_FOUND) :
ext-ms-windowscore-deviceinfo-l1-1-0.dll (NOT_FOUND) :

I also tried to follow this guide and I can confirm that os.path.exists('_multiarray_umath.pyd') returns True, but ctypes.WinDLL('_multiarray_umath.pyd') fails to load the DLL (or one of its dependencies).

Working example

Same steps as above but without executing mklvars.bat, so without linking to Intel MKL.

In the past this setup used to work with Python 3.6, VS2015 and a similar version of Intel MKL. I am probably missing something, any suggestion is more than welcome.

cubanpit

Solved by following a suggestion on Numpy mailing list (permalink).

Python >= 3.8 will no longer use PATH for resolving dependencies of extension modules. Use os.add_dll_directory(mkl_bin_path) https://docs.python.org/3/library/os.html#os.add_dll_directory in all your scripts before importing numpy or add the call to a _distributor_init.py file in the numpy package directory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pyinstaller numpy "Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll"

From Dev

Failed to Load DLL After Windows 10 Upgrade

From Dev

Python Opencv DLL load failed - Windows 10

From Dev

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

From Dev

DLL load failed - not a valid Win32 application - with NumPy

From Dev

NUnit failed to load DLL

From Dev

Tensorflow (1.13.1) Exception when import: DLL load failed (Windows 10)

From Dev

Tensorflow on windows - ImportError: DLL load failed: The specified module could not be found

From Dev

DLL load failed: the specified module could not be found Windows 10 shell

From Dev

Cannot load mkl_intel_thread.dll on python executable

From Dev

Compile numpy WITHOUT Intel MKL/BLAS/ATLAS/LAPACK

From Dev

Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll

From Dev

cx_Freeze unable fo find mkl: MKL FATAL ERROR: Cannot load mkl_intel_thread.dll

From Dev

The Module DLL C:\WINDOWS\system32\inetsrv\rewrite.dll failed to load

From Dev

Error "Module XYZ.dll failed to load" when using Regsvr32 to register a DLL on Windows 7

From Dev

PyInstallerImportError: Failed to load dynlib/dll?

From Dev

aspnetcore.dll failed to load

From Dev

DLL load failed python 3.7

From Dev

Anaconda issue, DLL load failed

From Dev

Error importing numpy from Anaconda, using conda (DLL load failed: The specified module could not be found)

From Dev

I am getting a "DLL load failed: The specified module could not be found." when using Cython with a Numpy program

From Java

How to install numpy+mkl for python 2.7 on windows 64 bit?

From Dev

import _ssl error,DLL load failed ,Python 37 Anaconda Windows 10

From Dev

Gradle sync failed: Can't load library windows-amd64\native-platform.dll

From Dev

Unable to debug Windows Store App ... A component dll failed to load. VS2015

From Dev

windows vim ":py import socket" returns "load dll failed: method not found"

From Dev

Tensorflow installation on Windows: DLL load failed while importing _pywrap_tensorflow_internal

From Dev

crash in Windows dll plugin on load

From

Golang, load Windows DLL in Linux

Related Related

  1. 1

    Pyinstaller numpy "Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll"

  2. 2

    Failed to Load DLL After Windows 10 Upgrade

  3. 3

    Python Opencv DLL load failed - Windows 10

  4. 4

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

  5. 5

    DLL load failed - not a valid Win32 application - with NumPy

  6. 6

    NUnit failed to load DLL

  7. 7

    Tensorflow (1.13.1) Exception when import: DLL load failed (Windows 10)

  8. 8

    Tensorflow on windows - ImportError: DLL load failed: The specified module could not be found

  9. 9

    DLL load failed: the specified module could not be found Windows 10 shell

  10. 10

    Cannot load mkl_intel_thread.dll on python executable

  11. 11

    Compile numpy WITHOUT Intel MKL/BLAS/ATLAS/LAPACK

  12. 12

    Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll

  13. 13

    cx_Freeze unable fo find mkl: MKL FATAL ERROR: Cannot load mkl_intel_thread.dll

  14. 14

    The Module DLL C:\WINDOWS\system32\inetsrv\rewrite.dll failed to load

  15. 15

    Error "Module XYZ.dll failed to load" when using Regsvr32 to register a DLL on Windows 7

  16. 16

    PyInstallerImportError: Failed to load dynlib/dll?

  17. 17

    aspnetcore.dll failed to load

  18. 18

    DLL load failed python 3.7

  19. 19

    Anaconda issue, DLL load failed

  20. 20

    Error importing numpy from Anaconda, using conda (DLL load failed: The specified module could not be found)

  21. 21

    I am getting a "DLL load failed: The specified module could not be found." when using Cython with a Numpy program

  22. 22

    How to install numpy+mkl for python 2.7 on windows 64 bit?

  23. 23

    import _ssl error,DLL load failed ,Python 37 Anaconda Windows 10

  24. 24

    Gradle sync failed: Can't load library windows-amd64\native-platform.dll

  25. 25

    Unable to debug Windows Store App ... A component dll failed to load. VS2015

  26. 26

    windows vim ":py import socket" returns "load dll failed: method not found"

  27. 27

    Tensorflow installation on Windows: DLL load failed while importing _pywrap_tensorflow_internal

  28. 28

    crash in Windows dll plugin on load

  29. 29

    Golang, load Windows DLL in Linux

HotTag

Archive