Import _tkinter # If this fails, Python cannot be configured for Tk

Some background information: I have windows 10 on my computer, and all programs are 64-bit versions.

I am writing a game in python (3.6.1) using tkinter, and now I would like to convert it to .exe. I used cx_freeze (5.0.1) and it did the build, but when I try to open the game, a window will open and then immediately close. So I tried to open it with cmd and the following message appeared:

File "sliks.py", line 1, in <module> File "C:\Users\Tinka\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 36, 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. 

I checked tkinter support as it says here: https://wiki.python.org/moin/TkInter and the error does not occur.

I also tried installing tk-dev with pip, as it says in some answers on this topic, but nothing happens when I get this message:

 C:\WINDOWS\system32>pip install tk-dev Collecting tk-dev Could not find a version that satisfies the requirement tk-dev (from versions: ) No matching distribution found for tk-dev 

I never had python 2.x on my computer, so there are no mixed libraries in this case: Error loading ImportError DLL with _tkinter import

Here is my setup.py file that I used for cx_freeze in case something is wrong:

 from cx_Freeze import setup, Executable import os os.environ['TCL_LIBRARY'] = r'C:\Users\Tinka\AppData\Local\Programs\Python\Python36\tcl\tcl8.6' os.environ['TK_LIBRARY'] = r'C:\Users\Tinka\AppData\Local\Programs\Python\Python36\tcl\tk8.6' base = None setup( name = "Six", version = "0.1", options = {"build_exe": {"packages": ["tkinter"]}}, executables = [Executable("sliks.py", base=base)] ) 

Any ideas what could be the problem? I know there are many open questions on this, but I tried most of the solutions and no luck.

+5
source share
1 answer

I had to get very carefully to understand this for myself. Not sure if this helps anyone, but it worked for me. From what I understand, these errors are generated when cx_freeze either cannot find all the dependencies or captures the incorrect ones.

The first thing I did was go to my python directory. Be VERY careful and make sure you look where your Python code is executing. Your IDE can give you this path if you do not know it. In cases with multiple installations or environments, you can disconnect.

Once there, I determined which file was causing the error. For my situation, it was a tkinter dependency. tcl86.dll and tk86.dll had problems. You can see the lines that I added. Then my logo actually started doing this, so I had to add this. Now it works great. Below is an example of my setup.py file (cx_freeze config).

 from cx_Freeze import setup, Executable import sys import os includes = [] include_files = [r"C:\Users\Ace\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll", r"C:\Users\Ace\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll", r"C:\Users\Ace\Desktop\IPNV\KP_App\FML\logo1.gif"] os.environ['TCL_LIBRARY'] = r'C:\Users\Ace\AppData\Local\Programs\Python\Python36\tcl\tcl8.6' os.environ['TK_LIBRARY'] = r'C:\Users\Ace\AppData\Local\Programs\Python\Python36\tcl\tk8.6' base = 'Win32GUI' if sys.platform == 'win32' else None setup(name='KpApp', version='0.9', description='KP Report App', options={"build_exe": {"includes": includes, "include_files": include_files}}, executables=[Executable(r'C:\Users\Ace\Desktop\IPNV\KP_App\FML\firstapp.py', base=base)]) 
+4
source

Source: https://habr.com/ru/post/1267062/


All Articles