1

I'm trying to create a standalone exe from a script writen in Python 3.5 using cx_Freeze to be able to run it on computers without Python.

The program itself is just a small calculator with a simple UI using tkinter. Im using Windows 10.

I created a setup script that looks like this.

import sys
from cx_Freeze import setup, Executable
# replaces commandline arg 'build'
sys.argv.append("build")

filename = "Widgets_tmp.py"
base = None
if sys.platform == "win32":
    base = "Win32GUI"
setup(
    name = "Widgets",
    version = "1.0",
#   options={"build_exe": {"packages": ["tkinter"]}},
    description = "cx_Freeze Tkinter script",
    executables = [Executable(filename, base=base)])

This runs without problems until i try to run the exe. Then I get this error:

Traceback (most recent call last):
  File "C:\python64\lib\site-packages\cx_freeze\initscripts\Console.py"
line 21, in <module>
    exec(code, m.__dict__)
  File "Widgets_tmp.py", line 1, in <module>
  File "C:\python64\lib\tkinter\__init__.py", line 35, in <module>
    import _tkinter#If this fails you Python may not be configured for Tk
ImportError: DLL load failed:

I tried including tkinter manualy in the code commented out in the code and in "includes" instead of "packages" but with the same results.

perhaps i should say that line 1 in Widgets_tmp.py looks like this:

import tkinter as tk

I tried freezing the sample code from the source but get the same error. Both codes work perfect using python.

I also tried using:

options={"build_exe": {"includes": ["tkinter"]}},

but no luck.

Brute
  • 11
  • 1
  • 4
  • Maybe you need to use the `--include-modules` command-line argument for `tkinter`. See http://stackoverflow.com/questions/2223128/cx-freeze-importerror-cannot-import-name – martineau May 24 '16 at 21:22
  • That question gave me the idea to try adding tkinter to the includes option and packages but that didnt work. Im quite new to both coding and using command-line again. The memories of DOS is comming back though. Should i simply add --include-modules to the "python Widgets_tmp.py Build" command or do i need to specify tkinter somehow? – Brute May 25 '16 at 06:15
  • The way I understood the answer to the linked question is that you need to include the `tkinter` module...which you do either by using an `--include-modules` command-line option OR by putting an `includes =` line in your setup.py script. – martineau May 25 '16 at 09:38
  • Just found the question [_creating .exe file with cx_freeze for a tkinter interface_](http://stackoverflow.com/questions/22004721/creating-exe-file-with-cx-freeze-for-a-tkinter-interface). Note how `tkinter` is specified on the `build_exe_options` line in the code in the accepted answer. – martineau May 25 '16 at 09:44
  • I tried with the `options={"build_exe": {"includes": ["tkinter"]}},` but with the same results. I also tried with options={"build_exe": {"packages": ["tkinter"]}}, as commented out in the code above. Same thing there. I will try using the command-line option as soon as I get home since Im at work at the moment. Perhaps Im doing something wrong with my `includes` but I cant seem to find it. – Brute May 25 '16 at 10:50
  • If all else fails, I'd try building the `SimpleTkApp.py` in the samples/Tkinter directory of [the source](https://bitbucket.org/anthony_tuininga/cx_freeze/src) using the `setup.py` there and see if the resulting .exe has a similar problem. – martineau May 25 '16 at 15:00
  • So i tried building the SimpleTkApp.py from the samples/Tkinter directory but that gave me the same error. it seems like the problem is importing Tkinter but I dont know whats wrong. I get no errors while building. The solutution found in other questions dont seem to work for me so Im out of ideas. Is there anyone out there who can help me figure out where to start looking? Maybe the question itself is missing some info about the problem? Im happy to add any info that might be needed. Im really stuck here., – Brute May 26 '16 at 06:13

1 Answers1

5

A tkinter exe properly ran by modifying setup.py on my PC.

OS = win7, Python = 3.5.2, cx_freeze = 5.0

setup.py:

includes      = []
include_files = [r"C:\Users\(USERNAME)\AppData\Local\Programs\Python\Python35-32\DLLs\tcl86t.dll", \
                 r"C:\Users\(USERNAME)\AppData\Local\Programs\Python\Python35-32\DLLs\tk86t.dll"]

setup(
    name = "Test",
    version = "1.0",
    options = {"build_exe": {"includes": includes, "include_files": include_files}},
    executables = [Executable("test.py", base=base)]
)

You must modify (USERNAME) for your environment.

aume
  • 51
  • 1
  • 2