Search code examples
python-2.7matplotlibcx-freeze

cx-freeze exe from python27


I am working with python27, Windows7 64bit, but my python version is 32bit to avoid some errors with different 64bit libraries. I struggle with the cx-freeze. I tried some easy examples and it works. Even if I import some self-written scripts into my main.py script it still works after I freeze it. The problem is if I add the matplotlib to the file. I know that is a common problem, but I can't solve it in my case. My normal plot.py script works, see code below. Don't worry about the different arrays, I also tried it with simple arrays like x = [1,2,3,4] and y= [1,2,3,4]. The impoert matplotlib as mpl and the line below is just to erase the toolbar, I thought it is maybe a reason it doesn't work, but it isn't.

import matplotlib.pyplot as plt
import csv
import numpy as np
import matplotlib as mpl
mpl.rcParams['toolbar'] = 'None'

# change delimiter to |
csv.register_dialect('pipes', delimiter='|')
# open file data.csv
csv_file_object = csv.reader(open("data.csv", 'r'), dialect='pipes')
# creates headers from the first line of the data file
header = csv_file_object.next()

data = []
for row in csv_file_object:
    data.append(row)
data = np.array(data)

y = []
for n1 in range(len(data)):
    y.append(float(data[n1][1]))
print y

time = []
for n2 in range(len(data)):
    time.append(int((data[n2][0].split('_')[1])[:4]))
print time


plt.plot(time, y)
plt.show()

I have tried the instruction from: enter link description here

but it didn't work. If I try this step by step the error raised if I try to execute the plot.exe is:

enter link description here

Maybe it is just an easy mistake and someone can help me, but I also tried it with other setup files and so on. I can freeze an easy program with a GUI, so it shouldn,t be a problem with Tkinter.

Hope someone can help me!

Cheers Max

edit: I found an example with a different backend which works see below link: enter link description here But I can't get my script running as an exe after I freezed it.


Solution

  • Okay I found a solution to solve the problem.

    My setup file looks like the following:

    import cx_Freeze
    import sys
    import matplotlib
    base = None
    if sys.platform == "win32":
        base = "Win32GUI"
    
    executables = [
        cx_Freeze.Executable("newtry.py", base = base),
        ]
    
    build_exe_options = {"includes":["FileDialog"],
                         "include_files":["test.csv"],
                         "excludes":[],
                         }
    cx_Freeze.setup(
        name = "script",
        options = {"build_exe": build_exe_options},
        version = "0.0",
        description = "A basic example",
        executables = executables)
    

    Thanks for the hint from Thomas K. about the FileDialog, because without the includes "FileDialog" it doesn't work.

    Furthermore to import the matplotlib in my executable file I have to use the following code:

    import matplotlib
    from matplotlib import pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    

    If I only use for example:

    import matplotlib.pyplot as plt
    

    it doesn't work. I don't know why but it doesn't? Anybody an answer?

    With this header and this setup.py file I can use matplotlib in my main program as:

    plt.plot([1,2,3])
    plt.show()
    

    I hope if someone has the same problem like me he or she can use my example.

    Cheers Max