Search code examples
pythonmatplotlibcx-freeze

Issue with matplotlib and cx_freeze


I'm trying to freeze a console-based program that uses matplotlib.pyplot to generate and save plots. (I don't need to preview or view the plots in anyway before they are saved.) Here's my setup.py script:

from cx_Freeze import setup, Executable
import os

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

setup(name='FLOUResence.exe',
    version='0.1',
    options = {"build_exe": {"packages":["pandas", "numpy", "scipy", "matplotlib"]}
           },
executables = [Executable(script='caller.py', targetName='FLOUResence.exe', 
icon="icon.ico", base='Console')]
)

I can compile the program, but when I run the graphing module it returns the following error:

This application failed to start because it could not find or load the Qt platform plugin "windows" in "".
Reinstalling the application may fix this problem.

From what I can tell, because matplotlib wants to load/use the Qt GUI, but because it's a console application cx_freeze doesn't load Qt? Is this a correct interpretation of the problem? Any thoughts on how to solve this problem?


Solution

  • You need to add the Qt platform plugins to your distribution directory. Give it a try and copy Library\plugins\platforms of the PyQt installation to your package/dist directory. If this works for you you can add the directory in your include_files build option. I'm using miniconda so the platforms directory is in c:\miniconda\Library\plugins.

    setup(name='FLOUResence.exe',
        version='0.1',
        options = {
            "build_exe": {"packages":["pandas", "numpy", "scipy", "matplotlib"],
                          "include_files": [r'c:\miniconda\Library\plugins\platforms']}
        },
        executables = [Executable(script='caller.py', targetName='FLOUResence.exe', 
                       icon="icon.ico", base='Console')]
    )