Search code examples
pythonnumpymatplotlibcx-freezeintel-mkl

Cannot load mkl_intel_thread.dll on python executable


I'm trying to create an executable python program that runs on windows without python being installed, for this I'm using cx_Freeze. But I get the following error: "Cannot load mkl_intel_thread.dll"

On my PC, which has python installed (miniconda3), I built the executable using cx_Freeze, and when I ran the executable I also would get "Cannot load mkl_intel_thread.dll". I fixed this by going to my python folder, Library\bin, and copied the mkl_intel_thread.dll file to where the executable is placed. The problem is, when moving the whole folder to another PC (without python installed), this error reappears, even though the mkl_intel_thread.dll is in the folder.

File that I want to distribute (plot.py):

import matplotlib.pyplot as plt

a = [0, 1, 2]
b = [0, 2, 0]
plt.fill(a, b, 'b')
plt.show()

cx_Freeze setup file (setup.py):

import cx_Freeze
import sys
import matplotlib
import numpy
import os

os.environ['TCL_LIBRARY'] = "C:\\Miniconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Miniconda3\\tcl\\tk8.6"


executables = [cx_Freeze.Executable("plot.py")]


build_exe_options = {"includes":['numpy.core._methods',
        'numpy.lib.format', 'matplotlib.backends.backend_tkagg']}

cx_Freeze.setup(
    name = "script",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "A basic example",
    executables = executables)

Solution

  • EDIT:

    1. Try to copy all files starting with mkl you find under Library\bin or numpy\core into the build folder, as well as libiomp5md.dll, see Python Pyinstaller 3.1 Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll and cx_freeze converted GUI-app (tkinter) crashes after pressing plot-Button.

      Once you have found out which file(s) need(s) to be manually copied, you can let cx_Freeze include the necessary file(s) by using the include_files list of the build_exe options (see code snippet below). If necessary, you can use a tuple (source, destination) as item in the include_files list to let cx_Freeze copy a file from source to a specific destination into the build directory, see the cx_Freezedocumentation.

    2. I see further potential problems in the setup script you've posted in your question:

      • include the whole numpy packages using the packages list of the build_exe options, it is easier and maybe safer
      • it is safer to dynamically find out the location of the TCL/TK DLLs
      • for cx_Freeze 5.1.1, the TCL/TK DLLs need to be included in a lib subdirectory of the build directory

    In summary, try t o use

    PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
    os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
    os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
    
    build_exe_options = {'packages': ['numpy'],
                         'includes': ['matplotlib.backends.backend_tkagg'],
                         'include_files': [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
                                            os.path.join('lib', 'tcl86t.dll')),
                                           (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
                                            os.path.join('lib', 'tk86t.dll'))
                                           # add here further files which need to be included as described in 1.
                                          ]}
    
    

    in your setup script.