Cx_Freeze building without error but exe doesn't open - python

I am attempting to build an exe using cx_Freeze which uses several modules:
import tkinter as tk
from tkinter import ttk
import random, time, bluetooth, json, sys, os
from _thread import *
from threading import Thread, Lock
When I attempt to build the exe, it seems to work perfectly: it raises no errors and creates the build folder containing the exe file. However, when I attempt to open the exe file, it simply doesn't open. If briefly seems to flash a windows but then disappears. My setup.py is this:
from cx_Freeze import setup,Executable
import sys
import os
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
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')
includes = []
include_files = []
packages = []
base = "Win32GUI"
setup(
name = 'Buzzer',
version = '0.1',
description = 'Buzzer application',
author = 'Me',
executables = [Executable('Buzzer.py')]
)
The flashing screen contains the following traceback:
Traceback (most recent call last):
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts__startup__.py", line 14, in run
module.run()
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.dict)
File "print.py", line 1, in
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 36, in
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.

You need to tell cx_Freeze to include the TCL and TK DLLs in the build directory.
For cx_Freeze 5.1.1 (the current version) or 5.1.0, the DLLs need to be included in a lib subdirectory of the build directory. You can do that by passing a tuple (source, destination) to the corresponding entry of the include_files list option:
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'))]
You need to pass this include_files list to the build_exe options in the setup call (and you should also pass the baseyou have defined to the Executable):
setup(
name = 'Buzzer',
version = '0.1',
description = 'Buzzer application',
author = 'Me',
options={'build_exe': {'include_files': include_files}},
executables = [Executable('Buzzer.py', base=base)]
)
For other cx_Freeze versions, the DLLs need to be included directly in the build directory. This can be done with:
include_files = [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]

Related

tkinter program compiles with cx_Freeze but program will not launch

I'm trying to create an executable following this tutorial
https://github.com/anthony-tuininga/cx_Freeze/tree/master/cx_Freeze/samples/Tkinter
After some tweaking I'm able to compile the project but when i click the .exe the mouse loading animation fires but nothing ever loads. This questions has been asked previously but was never resolved.
Where to start looking in the code when your .exe doesn't work after cx_freeze?
My app file
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('Button')
print("something")
new = messagebox.showinfo("Title", "A tk messagebox")
root.mainloop()
my setup.py
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [
Executable('SimpleTkApp.py', base=base)
]
setup(name='simple_Tkinter',
version='0.1',
description='Sample cx_Freeze Tkinter script',
executables= [Executable("SimpleTkApp.py", base=base)])
Also I have been manually adding the TCL/TK libraries
set TK_LIBRARY=C:\...\tk8.6 etc
My configuration: python 3.7, cx_Freeze 5.1.1
Any help would be greatly appreciated, I don't even know where to start on this one.
Try to modify you setup.py as follows:
import sys
from cx_Freeze import setup, Executable
import os
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'))]
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [Executable('SimpleTkApp.py', base=base)]
setup(name='simple_Tkinter',
version='0.1',
description='Sample cx_Freeze Tkinter script',
options={'build_exe': {'include_files': include_files}},
executables=executables)
This should work for cx_Freeze version 5.1.1 (the current version). In this version, the included modules are in a subdirectory lib of the build directory. If you use 5.0.1 or an earlier version, set
include_files = [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]
instead.
See also Getting "ImportError: DLL load failed: The specified module could not be found" when using cx_Freeze even with tcl86t.dll and tk86t.dll added in and python tkinter exe built with cx_Freeze for windows won't show GUI
EDIT:
A further problem is that cx_Freeze has a bug with python 3.7 which is not yet corrected. See Cx_freeze crashing Python3.7.0 . You can find there a link to a bug fix which you should apply manually (according to the OP this solved the problem, see comments).
After trying an even simpler hello world example writing to the console (which also failed) I stumbled across the culprit.
What could be the reason for fatal python error:initfsencoding:unable to load the file system codec?
After updating my freezer.py file with the code found here and using the setup.py provided by jpeg, my example app worked. Thank you both for your swift response.
I have a working setup.py here. Maybe you can try and see if it works after using the same config. Basically sometimes after compiling, the tk and tcl dll/packages are missing and so you need to include them during the setup.
import sys, os
from cx_Freeze import setup, Executable
includes = []
include_files = [r"C:\Users\user\AppData\Local\Programs\Python\Python36-32\DLLs\tcl86t.dll",
r"C:\Users\user\AppData\Local\Programs\Python\Python36-32\DLLs\tk86t.dll"]
base = 'Win32GUI' if sys.platform == 'win32' else None
os.environ['TCL_LIBRARY'] = r'C:\Users\user\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\user\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'
setup(name="simple_Tkinter",
version="0.1",
options={"build_exe":{"includes":[],"include_files":include_files}},
description="Sample cx_Freeze Tkinter script",
executables=[Executable("SimpleTkApp.py",base=base)])

error in creating executable file from python

i coded a python script in pycharm as "probe.py" and later built the executable(.exe) file out of it using the mentioned code in setup.py file but the exe file thus created shows error on opening as
import error mising required dependency['numpy'] even when it is present in my project.
error image
import sys
from cx_Freeze import setup,Executable
include_files = ['autorun.inf']
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name="Probe",version="0.1",description="fun",
options={'build_exe':{'include_files': include_files}},
executables=[Executable("probe.py",base=base)])
`
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
build_exe_options = {"packages": ["numpy"],
include_files = ['autorun.inf']}
setup(
name = "Probe",
version = "0.1",
description = "fun",
options = {"build_exe": build_exe_options},
executables = [Executable("probe.py",base=base)]
)
run this script tell me if there is a problem
According to cx_Freeze documentation, try adding build_exe with a packages key.

Using cx_Freeze to create executable (cannot import _tkinter, DLL load failed)

I am trying to create an executable from my Python project using cx_Freeze, but keep running into this error:
Here's my setup.py:
import cx_Freeze
import os, sys
os.environ['TCL_LIBRARY'] = "D:\\Code\\Python\\3.5.2\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "D:\\Code\\Python\\3.5.2\\tcl\\tk8.6"
base = None
if sys.platform == "win32":
base = "Win32GUI"
executables = [cx_Freeze.Executable("Main.pyw", base=base)]
includes = ["tkinter"]
include_files = [r"D:\Code\Python\3.5.2\tcl\DLLs\tcl86t.dll", \
r"D:\Code\Python\3.5.2\tcl\DLLs\tk86t.dll"]
cx_Freeze.setup(
name="Test",
version = "1.0",
options={"Test.exe": {"packages":["pygame", "numpy"], "includes": includes, "include_files": include_files}},
executables = executables)
I tried putting tkinter in the "packages" list, but still get the same error. I also checked other StackOverflow posts and used parts of their setup.py code into mine, but nothing is working. I can't use PyInstaller as it doesn't support pygame and py2exe doesn't support Python 3.5. Any help would be appreciated.
Copy and Paste the files (tcl86t.dll, tk86t.dll) to the exe.win .
It worked for me.

Running Executable made with cx_Freeze giving traceback error

Here is my setup.py file for cx_Freeze
import sys
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r"C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = r"C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\tcl\tk8.6"
build_exe_options = {"packages":["os"], "includes" : ["tkinter"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name='APS West Email Generator',
version='1.0',
description='Auto generates Alarm notification emails. And maybe more in the future.',
options = {"build_exe": build_exe_options},
executables = [Executable("Tool_EmailGenerator.py", base=base)])
The executable is made with no errors. But when I try to run it I get the following window:
title: cx_Freeze: Python error in main script
contents:
Traceback(most recent call last):
file
"c:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cx_Freeze\initscripts__startup__.py",line12,in import(name+ "init")
file
"c:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cx_Freeze\initscripts\Console.py",line 21, in scriptModule=import(moduleName)
File "Tool_EmailGenerator.py", line 2, in
File
"c:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter__init__.py", line 35, in
import_tkinter #if this fails your python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.
tkinter works when I run the py file pre-cx_Freeze. I've searched the internet and tried various suggestions. nothings worked so far. I'm not sure whats causing this.
managed to fix this issue.
Instead of doing:
"includes" : ["tkinter"],
I added tkinter to the packages.
so: "packages":["os", "tkinter"]
that seemed to work

KeyError: 'TCL_Library' when I use cx_Freeze

When I use cx_Freeze I get a keyerror KeyError: 'TCL_Library'while building my pygame program. Why do I get this and how do I fix it?
My setup.py is below:
from cx_Freeze import setup, Executable
setup(
name = "Snakes and Ladders",
version = "0.9",
author = "Adam",
author_email = "Omitted",
options = {"build_exe": {"packages":["pygame"],
"include_files": ["main.py", "squares.py",
"pictures/Base Dice.png", "pictures/Dice 1.png",
"pictures/Dice 2.png", "pictures/Dice 3.png",
"pictures/Dice 4.png", "pictures/Dice 5.png",
"pictures/Dice 6.png"]}},
executables = [Executable("run.py")],
)
You can work around this error by setting the environment variables manually:
set TCL_LIBRARY=C:\Program Files\Python35-32\tcl\tcl8.6
set TK_LIBRARY=C:\Program Files\Python35-32\tcl\tk8.6
You can also do that in the setup.py script:
os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tk8.6'
setup([..])
But I found that actually running the program doesn't work. On the cx_freeze mailinglist it was mentioned:
I have looked into it already and no, it is not just a simple recompile --
or it would have been done already! :-)
It is in progress and it looks like it will take a bit of effort. Some of
the code in place to handle things like extension modules inside packages
is falling over -- and that may be better solved by dropping that code and
forcing the package outside the zip file (another pull request that needs
to be absorbed). I should have some time next week and the week following
to look into this further. So all things working out well I should put out
a new version of cx_Freeze before the end of the year.
But perhaps you have more luck ... Here's the bug report.
Instead of setting the environment variables using installation specific absolute paths like C:\\LOCAL_TO_PYTHON\\... you may also derive the necessary paths dynamically using the __file__ attribute of Python standard package like os:
import os.path
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
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')
After this fix the executable file will be created, but you will probably get a "DLL not found error" when you try to execute it - at least with Python 3.5.3 and cx_Freeze 5.0.1 on Windows 10.
When you add the following options, the necessary DLL-files will be copied automatically from the Python-Installation directory to the build-output of cx-Freeze and you should be able to run your Tcl/Tk application:
options = {
'build_exe': {
'include_files':[
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
],
},
}
# ...
setup(options = options,
# ...
)
Just put this before the setup at setup.py
import os
os.environ['TCL_LIBRARY'] = "C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tk8.6"
And run it:
python setup.py bdist_msi
This worked fine for me.
If you get following error with python 3.6:
copying C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6 -> build\exe.win-amd64-3.6\tcl
error: [Errno 2] No such file or directory: 'C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6'
Simply create LOCAL_TO_PYTHON dir in C:\ then create Python35-32 dir inside it. Now copy tcl dir from existing Python36 dir (in C:\) into Python35-32.
Then it works fine.
If you get following error with python 3.6:
copying C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6 -> build\exe.win-amd64-3.6\tcl
error: [Errno 2] No such file or directory: 'C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6'
Simply create LOCAL_TO_PYTHON dir in C:\ then create Python35-32 dir inside it. Now copy tcl dir from existing Python36 dir (in C:) into Python35-32.
Then it works fine.
**I did this steps and created a .exe file into the build dir but if ı try to click app dont wait on the screen instantly quick, my codes here **
from tkinter import *
import socket
window=Tk()
window.geometry("400x150")
window.title("IpConfiger")
window.config(background="black")
def goster():
x=socket.gethostbyname(socket.gethostname())
label=Label(window,text=x,fg="green",font=("Helvetica",16))
label.pack()
def information():
info=Label(window,text="Bu program anlık ip değerini
bastırır.",fg="green",font=("Helvetica",16),bg="black")
info.pack()
information()
tikla=Button(window,text="ip göster",command=goster)
tikla.pack()
D. L. Müller's answer need to be modified for cx_Freeze version 5.1.1 or 5.1.0. In these versions of cx_Freeze, packages get frozen into a subdirectory lib of the build directory. The TCL and TK DLLs need to be moved there as well. This can be achieved by passing a tuple (source, destination) to the corresponding entry of the include_files list option (see the cx_Freeze documentation).
Altogether the setup.py script needs to be modified as follows:
import os.path
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
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')
# ...
options = {
'build_exe': {
'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'))
],
},
}
# ...
setup(options = options,
# ...
)
The initial KeyError problem:
This worked for me with python 3.7 on windows 7:
from cx_Freeze import setup, Executable
import os
import sys
where = os.path.dirname(sys.executable)
os.environ['TCL_LIBRARY'] = where+"\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = where+"\\tcl\\tk8.6"
build_exe_options = {"include_files": [where+"\\DLLs\\tcl86t.dll", where+"\\DLLs\\tk86t.dll"]}
setup(
name = "SudoCool",
version = "0.1",
description = "Programme de SUDOKU",
options={"build_exe": build_exe_options},
executables = [Executable("sudoku.py")]
)
Now cx_Freeze is working:
My application is working:

Categories

Resources