3

I am programming on Win 7 in Spyder/Anaconda. And I am having trouble converting my py into an exe. For background, my program .py has a few csv files it takes data from, asks the user for 4 integers, and generates a plot in matplotlib. It imports the packages below.

I was able to execute something like this (How can I convert a .py to .exe for Python?) but my situation isn't working when I start to use my code.

If I include "matplotlib" into the packages list, I get "KeyError: 'TCL_Library". What is this error and how do I fix it? Adding "os" works for reference.

In my program py, I use: import os, from os import listdir, import pylab, import matplotlib.pyplot as plt, import numpy as np, import matplotlib, import random. Do I leave these in my program py or move them to setup and how do I include the "from xxx" items in the packages array?

import os
from cx_Freeze import setup, Executable

base = None    

executables = [Executable("try1.py", base=base)]

cwd = os.getcwd()
f_3_to_3=cwd+'\\' +  '3_to_3.csv'

packages = ["idna", "matplotlib"]
options = {
    'build_exe': {  
        "include_files": (f_3_to_3),
        'packages':packages,
    },    
}

setup(
    name = "FirstBuild",
    options = options,
    version = "0",
    description = 'This is cool',
    executables = executables
)
Alex
  • 87
  • 7
  • 1
    Possible duplicate of [Issue with matplotlib and cx\_freeze](https://stackoverflow.com/questions/42078262/issue-with-matplotlib-and-cx-freeze) – sophros Aug 06 '18 at 17:57
  • 1
    Have you checked [this example code](https://github.com/anthony-tuininga/cx_Freeze/tree/master/cx_Freeze/samples/matplotlib)? – sophros Aug 06 '18 at 17:57
  • that example code lead me to the solution, thank you – Alex Aug 07 '18 at 11:38
  • 1
    Great! I wouldn't mind you upvoting the comment... – sophros Aug 07 '18 at 14:42

1 Answers1

1

Using this for my setup file worked. Note, I had to fix directory for tk and tcl along with reinstalling them

import os
from cx_Freeze import setup, Executable
import sys

base = None

os.environ['TCL_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\Library\\lib\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\Library\\lib\\tk8.6"

executables = [Executable("MyPyFile.py", base=base)]

packages = ["idna", "os", "numpy", "numpy.core._methods", "matplotlib", "random"]
options = {
    'build_exe': {
        "includes": ["numpy.core._methods", "numpy", "tkinter"],
        "include_files": [r'C:\ProgramData\Anaconda3\Library\plugins\platforms'],
        'packages':packages,
    },    
}

setup(
    name = "FirstBuild",
    options = options,
    version = "0",
    description = 'This is cool',
    executables = executables
)
Alex
  • 87
  • 7