Environment

Program

Version

 Window window7

 python

 Python 3.6.4

 cx_freeze 

 5.1.1




Troubleshooting

Execution

Command 

Description

 python 으로 실행시

 python example.py

 정상적으로 실행됨

 executable file로 실행시

 example.exe

error 1)

 tk_agg error


error 2)

 import _tkinter # If this fails your Python may not be configured for Tk



Solution

1. Source Code
1
2
3
4
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
cs


2. Setup Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import matplotlib
from cx_Freeze import setup, Executable
from pip._vendor.requests import packages
 
# <added>
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')
 
 
if __name__ == '__main__':
    build_exe_options = dict(
            #compressed = True,
            packages = ["matplotlib","tkinter"],
            includes = ["sys","matplotlib.pyplot","numpy.core._methods","numpy.lib.format"],
            #include_files = []
            include_files = [r"C:\Python\Python36\DLLs\tcl86t.dll",
                 r"C:\Python\Python36\DLLs\tk86t.dll"]
    )
 
    base = None
    if sys.platform == "win32":
        base = "Win32GUI"
 
    setup(
        name = "HelloTest",
        version = "1.0",
        author="kissuu",
        description = "It is test code to check matplotlib",
        options = {"build_exe": build_exe_options},
        #options = {"build_exe": {"packages":["tkinter"]}},
        #options={"build_exe": {"includes": includes, "include_files": include_files}},
        executables = [Executable("../../Hello/src/Hello.py", base=base, targetName="hello.exe")]
    )
cs



Ref.

https://stackoverflow.com/questions/43568915/import-tkinter-if-this-fails-your-python-may-not-be-configured-for-tk



'Language > Python' 카테고리의 다른 글

[Python] 추천 IDE  (0) 2020.03.23
Posted by kissuu
,