11

When using cx_Freeze and Tkinter, I am given the message:

File "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.

Some things to note:

  • I want to use Python 3+ (Currently using 3.5.3, 32-bit). Don't really care about a specific version, whatever works.
  • My project has multiple files I need to compile. As far as I can tell, that leaves me with cx_Freeze or Nuitka. Nuitka had problems of its own.
  • I am using Windows 10 Home Edition, 64-bit

Here is my current setup.py:

from cx_Freeze import setup, Executable    
import sys  

build_exe_options = {"packages": ["files", "tools"]}  

base = None    
if sys.platform == "win32":    
    base = "Win32GUI"    

setup(name="Name",  
      version="1.0",  
      description="Description",  
      options={"build_exe": build_exe_options},  
      executables=[Executable("main.py", base=base)],  
      package_dir={'': ''},  
      )

I have tried many solutions from all corners of the internet. Including but not limited to:

  • Multiple versions of python (and the corresponding cx_Freeze/Tkinter versions)
  • Both 32-bit and 64-bit versions
  • Replacing Tkinter with easygui (apparently easygui needs Tkinter to work)
  • Checking the PATH variables
  • Restarting my computer (Don't know what I expected)
  • Uninstalling other versions of python and repairing the correct version
  • Placing the following in my compile bat file (Definetly the correct paths):

    set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\tcl\tcl8.6
    set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\tcl\tk8.6
    
  • Placing the following in my setup.py:

    options={"build_exe": {"includes": ["tkinter"]}}
  • Along with:
    include_files = [r"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\DLLs\tcl86t.dll",\
                     r"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\DLLs\tk86t.dll"]

(And yes, those were included in setup() in one way or another)


Thanks for any help, it's greatly appreciated. And yes, I have looked at just about every solution to this problem on this site. Hoping someone could help me find yet another solution since my problem seems to be persistent.

Reece Mathews
  • 381
  • 1
  • 2
  • 11

3 Answers3

17

Found a solution!

I had to copy the tk86t.dll and tcl86t.dll files from my python directory's DLLs folder into the build folder with the main.py I was trying to compile.

This, in conjunction with having

set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35\tcl\tcl8.6  
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35\tcl\tk8.6

at the top of my compile.bat, and including
"include_files": ["tcl86t.dll", "tk86t.dll"]
in my build_exe_options in setup.py, seems to have done the trick.

Here is my current setup.py:

from cx_Freeze import setup, Executable  
import sys  

build_exe_options = {"packages": ["files", "tools"], "include_files": ["tcl86t.dll", "tk86t.dll"]}  

base = None  
if sys.platform == "win32":  
    base = "Win32GUI"  

setup(name="Name",  
    version="1.0",  
    description="Description",  
    options={"build_exe": build_exe_options},  
    executables=[Executable("main.py", base=base)],  
    package_dir={'': ''},  
    )  

And here is my compile.bat (updated to show all steps):

@echo off
set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6
RD /S /Q "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin"
mkdir "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin"
xcopy /s "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\DLLs\tcl86t.dll" "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin\tcl86t.dll"
xcopy /s "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\DLLs\tk86t.dll" "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin\tk86t.dll"
cd "C:\Users\VergilTheHuragok\Desktop\PythonProject\"
cxfreeze main.py --target-dir "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin" --target-name "launch.exe"
pause  

I found this solution here.

Reece Mathews
  • 381
  • 1
  • 2
  • 11
  • Ignore any discrepancies between python versions in the answer and the original post. I tried many different versions before finding this solution. I do not believe the version was the problem. – Reece Mathews Feb 21 '17 at 03:55
  • Could you shed light on compile.bat please? Where do I find it from? And should I copy my main.py code into build folder along with dll's? – bit_scientist Aug 01 '18 at 03:56
  • @voo_doo I updated the post to show my full compile.bat file. This was a while ago but I'm guessing I found an example compile.bat for cx_freeze and then altered it to fit my needs. You shouldn't need to copy your main.py file into the build folder -- cx_freeze just needs the path to the main.py file passed as its first argument. It will create the compiled version wherever you specify via the target-dir argument. – Reece Mathews Aug 02 '18 at 04:22
  • thank you for your efforts. I couldn't find any compile.bat file anywhere. Where is it? – bit_scientist Aug 02 '18 at 04:29
  • 1
    @voo_doo You'll have to make your own. Just create a text file on your desktop or wherever works best and rename it 'compile.bat' (the name doesn't actually matter as long as it's a .bat). Then paste in the code I posted above and edit it to fit your needs or rewrite it how you like. – Reece Mathews Aug 02 '18 at 05:04
  • For `cx_Freeze` version 5.1.1, the DLLs need to be copied into a subdirectory `lib` of the build directory, see [this answer](https://stackoverflow.com/a/52257858/8516269). – jpeg Sep 10 '18 at 19:35
  • Thanks reece for your excellent response. I got the error "no module named files" so I removed that from the build_exe_options and it seems to have worked fine for a basic tkinter module. – Lionel Yu Sep 17 '18 at 21:59
0

image

to solve this problem just copy the files 1.tcl86t.dll 2.tk86t.dll from this path C:\Users\h280126\AppData\Local\Programs\Python\Python36-32\DLLs and placed in our .exe path C:\Users\h280126\PycharmProjects\my_tool\build\exe.win32-3.6 it is working fine :)

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
0

After fixing these issues cx_freeze was still unable to import the dependencies of pandas (namely numpy). To fix this I literally copied and pasted the entire folders into the directory of the .py file I was trying to compile. The executable needs to be in the same directory (so it isn't necessarily stand-alone) but it runs with pandas and numpy.

cccnrc
  • 1,195
  • 11
  • 27