1

This is my first post here, so if I make a mistake please tell me, I'll correct it. I am in python 3.6, windows 10, I have a program that I need to compile with cx_Freeze. I cannot get my setup.py to work, it has an error when I try to compile. The program I am trying to compile starts with:

import pygame
from pygame.locals import *
import sys
import time
import tkinter
from tkinter import filedialog
from tkinter import messagebox

I need all of these to make the program work, yet I need to compile it with cx_Freeze, Somebody please help me!

My setup.py is

from cx_Freeze import setup, Executable

base = None

executables = [Executable("to-compile.py", base=base)]

packages = ["idna","os","sys","tkinter","pygame"]
options = {'build_exe' : {'packages':packages}}

setup(name="<any name>",options=options,version="<any number>",description="<any description>",executables=executables)

I have a compiler.bat that contains:

python setup.py build

And my error is: Powershell Error

Seems like I cannot insert images yet I need a reputation.

PyInstaller does not work:

I will post error code on pastebin

If there is a solution to the problem with py2exe(or whatever variation of that compiler), please tell me just keep in mind that I am in python 3.

PythonPro
  • 291
  • 2
  • 11
  • 3
    You need to edit your question properly and provide us with the errors you have faced. – pvy4917 Nov 12 '18 at 19:54
  • 1
    I can use [PyInstaller](https://pythonhosted.org/PyInstaller/usage.html) to build binaries without trouble. It starts as easy as `pyinstaller my_script.py`. – import random Nov 12 '18 at 21:42
  • Please provide your error message (as text please). See [here](https://stackoverflow.com/q/22004721/8516269) how to freeze a `tkinter` based application with `cx_Freeze`. – jpeg Nov 13 '18 at 08:22
  • @Eric I will try PyInstaller out. – PythonPro Nov 13 '18 at 21:21
  • @karma4917 I have edited the question and included my error message. Am I missing anything else? – PythonPro Nov 13 '18 at 21:22

1 Answers1

1

You need to set the environment variables TCL_DIRECTORY and TK_DIRECTORY and to tell cx_Freeze to include the Tcl and Tk DLLs using the build_exe option include_files as done in this answer. If you are using cx_Freeze 5.1.1 or 5.1.0, you need to do it slightly differently, see this answer.

Furthermore, you should set base = "Win32GUI" for GUI applications under Windows.

In summary, assuming you are using cx_Freeze 5.1.1 (the current version), try to use the following setup script:

from cx_Freeze import setup, Executable

import os
import sys
PYTHON_INSTALL_DIR = os.path.dirname(sys.executable)
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

include_files = [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
                 (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))]
packages = ["idna","os","sys","tkinter","pygame"]
options = {'build_exe' : {'packages':packages, 'include_files':include_files}}

# GUI applications require a different base on Windows (the default is for a console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

executables = [Executable("to-compile.py", base=base)]

setup(name="<any name>",options=options,version="0.1",description="<any description>",executables=executables)
jpeg
  • 2,372
  • 4
  • 18
  • 31
  • Ok I will try it. Also I need to include the time module so how do I include that? Edit: And I need sys too. – PythonPro Nov 15 '18 at 22:25
  • @PythonPro According to the experiences I've made so far, `cx_Freeze` includes the `time` module correctly, you don't need to include it manually. Just import it in the main script where you use it, as you have shown at the beginning of your question. As far as `sys` is concerned, I had forgotten an `import sys` in the setup script of my answer, I've corrected that now (line 4), sorry. – jpeg Nov 16 '18 at 06:43
  • It doesn't work. There's and error when running setup() - error https://pastebin.com/raw/9zG3B3Kk – PythonPro Nov 21 '18 at 18:45
  • @PythonPro In the setup script, the version argument of `setup` should read `version` instead of `versions`and the given value should match a specific pattern (something like at most 3 digits separated by dots, the exact rule can be found in the source code of `distutils`). I've corrected my answer accordingly. – jpeg Nov 21 '18 at 20:24