Running Python 3.5.3 and the latest cx_Freeze. When building the exe, everything runs fine. However, when I run the exe I get a dialogue message saying:
# If this fails your Python may no be configured for Tk
ImportError: DLL load failed: The specified module could not be found.
I followed the solution found here (the second answer using the the file method rather than explicitly specifying the path -- which is what I tried first with the same result). And also tried the solution found here, but I'm still getting the error when I run the exe.
Here's my setup.py file:
import os
import sys
from cx_Freeze import setup, Executable
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(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
],
},
}
setup(
name="Drawings Converter",
version="1.0",
description="Converts PDF files named by part number to PDF files named by reference number",
executables=[Executable("DrawingsConverter.py", base="Win32GUI")])
Anyone have an idea of what I might be missing? This is the first time I've used cx-Freeze, and I'm also quite new to Python. Any detailed info on the problem that would help me learn to avoid this in the future would be very appreciated. Thanks in advance!
EDIT: I solved the issue by copying the .dll files over to the build directory. However, I would still appreciate it if anyone could give me any insight as to why cx_Freeze was not doing this or could not find them.
Solved by manually copying the .dll files over to the build directory.
Related
PROBLEM SOLVED.
the issue was with jaraco module, that i used for clipboard manipulation, i used pyperclip instead.
I made a python app with tkinter that works fine, but I wanted to make an exe from it so it's user friendly in windows. I used cx_Freeze library for that, that works fine too, but not always.
When creating the exe using cx_Freeze you can specify base parameter, if you give none that will open 2 windows, cmd window and a GUI window for your app. To get rid of the cmd window you can specify "Win32GUI" as base parameter for cx_Freeze.
This ignores the cmd window and just opens the GUI, it seems to be working, but not always.
Sometimes opening the exe file will start the proccess but no GUI will show. Opening cmd and going to the directory of your exe and starting it from there will actually show the GUI and fix the problem untill you restart your pc (you can open the app without cmd with everything working untill restart)
It seems that as long as cmd window is in your ram, the GUI will show, otherwise it "doesn't know" and fails to show GUI.
The code can be found here:
https://github.com/GrishaDev/ClipMagic
clip.py
is the entire app
setup.py
is the file used with cx_Freeze to get exe of the app, that's where you specify base parameter and such.
the piece of code where the problem most likely is (setup.py):
import sys
from cx_Freeze import setup, Executable
# import os
# os.environ['TCL_LIBRARY'] = "C:\\Users\\Grisha\\AppData\\Local\\Programs\\Python\\Python35\\tcl\\tcl8.6"
# os.environ['TK_LIBRARY'] = "C:\\Users\\Grisha\\AppData\\Local\\Programs\\Python\\Python35\\tcl\\tk8.6"
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')
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name="clipmagic",
version="1",
description="Extended clipboard",
options={'build_exe': {'includes': ["jaraco", "tkinter"], 'include_files':[
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
'icon.ico',
]}},
executables=[Executable("clip.py", base=base, icon='icon.ico')])
#"Win32GUI"
Thanks!
Looking at the README.md in your code repository, you are using the current version of cx_Freeze, which is 5.1.1. In this version, the included modules are in a subdirectory lib of the build directory. The manually added DLLs apparently need to be moved there as well. See this answer.
Try to make the following change to your setup.py script:
options={'build_exe': {'includes': ["jaraco", "tkinter"], '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')),
'icon.ico',
]}},
I am trying to use cx_Freeze to make an executable for my program.
Even though the python program works perfectly, the executable says it cant file file_cleaner.
This is what the setup.py looks like
import sys
from cx_Freeze import setup, Executable
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')
build_exe_options = {"packages": ["os", "glob", "itertools", "numpy",
"matplotlib", "lmfit", "pandas", "scipy", "pathlib"], "includes": [
"src\\BCS_fit.py", "src\\file_cleaner.py", "src\\__init__.py",
"src\\dependencies\\"], "include_files": ["test\\"]}
base = None
setup(name="BCS processor",
version="0.1",
description="Console application for processing VTS data and fitting it
according to BCS theory",
author="Anil Radhakrishnan",
options={"build_exe": build_exe_options},
executables=[Executable("src\\Master.py", base=base)])
BCS_fit.py and file_cleaner.py are 2 other python files that I call from master.py.
The Dependencies folder has .py and .pyd file from a c module converted to python.
This is the first time I am trying to create an executable for a python script, please excuse any beginner errors.
Thanks a lot for your assistance!
In the past I've used py2exe, not sure if it works with Python 3.6 though,so depends what version you are currently using.
This SO has a quick guide to using cx_Freeze, maybe it covers something that you may have missed out?
Also Pynsist seems to be another very good way of creating an executable from a Python script. It can also create installation files for your program.
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:
I was wondering if there is a way to reduce file size when creating a Python exe using cx_freeze or something else.
Excluding unnecessary modules is suggested before, but rather than that, I am trying to find if it is possible to actually exclude parts of the Python itself.
When building the exe, I observed many stuff I didn't need was included in the build file (Almost all of Lib directory, many stuff from site-packages etc). I ask if we can exclude these ? Or better, can I only include only the things I need. (need to find what I need first)
And for even more, is it possible to exclude unused built-in features of Python itself ? even if it means rebuilding Python from source ?
Thanks for your time.
Edit: Maybe they are already being excluded, I may have misinterpreted the situation. But I had to manually exclude Tkinter. Which made my question if more could be removed.
Edit2: Here is the script file:
from cx_Freeze import setup, Executable
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')
base = None
executables = [Executable("serdar.py", base=base)]
includefiles = ['zombie.png', 'human.jpeg']
packages = []
excludes = ["tkinter"]
options = {
'build_exe': {
'packages':packages,
'excludes':excludes,
'include_files':includefiles,
"optimize": 2
},
}
setup(
name = "serdar",
options = options,
version = "0.01",
description = 'None',
executables = executables
)
In setup.py try include only packages which you want as in example:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
from documentation: "A common problem is that cx_Freeze hasn’t automatically detected that a file needs to be copied. Modules that your code imports are detected, but if they’re dynamically loaded - e.g. by a plugin system - you have to tell cx_Freeze about them. "
i am trying to convert my python script to a exe file
that anyone can run it from any computer, including computers with no python.
so i see some guides that explain the best way is to use in the cx_freeze library.
so i built a small gui application that use in tkinter only, this is my code:
import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
and this is my setup file:
from cx_Freeze import setup, Executable
setup(
name="GUI PROGRAM",
version="0.1",
description="MyEXE",
executables=[Executable("try.py", base="Win32GUI")],
)
and i run this command:
python setup.py build
and then i get this error:
KeyError: 'TCL_LIBRARY
and it only happens when i use tkinter. so i guess i miss something and i need to add in some way the tkinter to the setup file.
can someone help me?
thanks lot you guys.
Try you altering your setup script to this:
from cx_Freeze import setup, Executable
import os
import sys
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')
files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path To Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}
setup(
name="GUI PROGRAM",
version="0.1",
description="MyEXE",
options = {"build_exe": files},
executables=[Executable("try.py", base="Win32GUI")],
)
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') will remove the error message, while files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path To Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]} will include the missing Tk and Tcl runtimes.