Search code examples
pythonmatplotlibcx-freeze

Cx_freeze .exe wont work when moved


I have made a cx_freexe .exe from a set of scripts which works fine in the original build folder. However, when I move it to our analysis computer with a bit more muscle, I get the following error:

C:\Program Files\Davidek\Davidek beta 1.2.0>Davidek.exe
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__start
up__.py", line 12, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console
.py", line 24, in <module>
  File "Davidek.py", line 11, in <module>
  File "C:\Users\Max\Scilifelab\Projects\Master thesis\Davidek\Code\TICwriter.py
", line 7, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py", line
 122, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook.py", line 32
, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py", line 142,
 in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 1
3, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line
8, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\type_check.py", lin
e 11, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\__init__.py", line
 14, in <module>
ImportError: DLL load failed: The specified module could not be found.

C:\Program Files\Davidek\Davidek beta 1.2.0>PAUSE
Press any key to continue . . .

This is the code "TICwriter.py" that seems to cause the problem:

import matplotlib
#Forces matplotlib to not show plot:
matplotlib.use('Agg')
import matplotlib.pyplot as pl    
import os

def TICwriter(TIC, dataFile, saveDirectory):
    """ Saves a TIC file as a .png in the specified saveDirectory under a 
        output subdirectory.
    """
    #Create savename from data file name:
    savefile =  dataFile.split('/')[-1].split('.')[0] + '_TIC.png'
    #Create ouput directory:
    saveDirectory = os.path.join(saveDirectory, 'output/')
    os.makedirs(os.path.dirname(saveDirectory), exist_ok=True)
    #Plot figure:
    Plot = pl.figure()
    TICplot = Plot.add_subplot(111)
    TICplot.plot([d[0] for d in TIC], [d[1] for d in TIC])

    #Save and close plot:
    pl.savefig(saveDirectory + savefile)
    pl.close(Plot)

It seems to me like matplotlib is causing the problem as the error occurs at line 7:

import matplotlib

My build script looks like this:

import sys
from os import environ
from os.path import dirname
from cx_Freeze import setup, Executable
import scipy
scipy_path = dirname(scipy.__file__)


# Set the TCL and TK library explicitly (it seems like the python 3.6 causes
# errors otherwise):
environ['TCL_LIBRARY'] = r'C:\ProgramData\Anaconda3\tcl\tcl8.6'
environ['TK_LIBRARY'] = r'C:\ProgramData\Anaconda3\tcl\tk8.6'


#Inclusion of dll files to fix tkinter import:
include_files = [r'C:\ProgramData\Anaconda3\DLLs\tcl86t.dll', 
                 r'C:\ProgramData\Anaconda3\DLLs\tk86t.dll', 
                 scipy_path]
#Inclusion of modules that need to be explicitly imported for some reason:
packages = []#['pyteomics']

#Dependencies that are not implicitly detected:
build_exe_options = {'includes': ['numpy.core._methods', 'numpy.lib.format', 'numpy.matlib'], 
                     'excludes': [],
                     'include_files': include_files, 
                     'packages': packages}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = "Console"

#if sys.platform == 'win32':
#    base = 'Win32GUI'

setup(  name = 'Davidek beta 1.1.1',
        version = '1.1.1',
        options = {'build_exe': build_exe_options},
        executables = [Executable('Davidek.py', base=base)])

It seems to me that the .exe tries to look for the matplotlib module in the original folder from the computer where the executable was created even though matplotlib is included in the build folder. The .exe runs fine whenever I run it from the original computer, even if I move the build from the original folder. I am using python 3.6 on 64-bit Windows 10. Any advice on this would be greatly appreciated. Let me know if any more information is required.

Thanks.


Solution

  • I managed to fix it, so here we go for anyone with this problem in the future: The issue can be fixed by copying the following files from "C:\ProgramData\Anaconda3\Library\bin" (or "...\Anaconda3\Library\bin") to your build folder:

    • mkl_core.dll
    • mkl_intel_thread.dll
    • mkl_def.dll
    • libiomp5md.dll