Summary of the problem solving process of "No module named _tkinter" under Python __python

Source: Internet
Author: User
Tags in python

Introduction: Under the Python3 run Matplotlib, encountered the "No module named _tkinter" Problem, spent several hours to study the solution, here said the entire process recorded, and try to analyze the process of solving ideas and pros and cons, for the AfterEffect, This highlights the need to focus on the analysis of the error message, this is the first scene. Environment Introduction

Any technical problems and fixes are dependent on the system environment and a specific version, described here first:

ubuntu:17.10
python:3.6.1 based on virutalenv to switch between different Python environments tkinter problem Description

The original program is based on Matplotlib to do the drawing operation, which in the course of operation, reported out the error message. Since most of the original program has nothing to do with the problem itself, and the log itself is much more, here is the only key information to be intercepted:

Import _tkinter # If This fails your Python may is configured for Tk
preliminary analysis on named _tkinter of Importerror:no module

Use Python friends know, encountered similar problems, such as import Error, the approximate rate is a package is not installed, so the first reaction is a package missing, so first the package and class library query:

PIP3 Search Tkinter

The results found that a large number of packages were matched to, and that some of the associated packages had tkinter, and that information was largely not helpful.

..................

TKINTER.HELP (2.0)-Small Preview of Tkinter Widgets tkinter3000

(1.1-20051211)-Widget construction Kit for Tkinter

tkinterhtml (0.7)-Python wrapper for TKHTML3 (http://tkhtml.tcl.tk/)

tkinterquickhelper (1.5.18)-Helpers for Tkinter, extra windows.

Tkintertable (1.2)-Extendable table class for Tkinter ....

.........

So try to install tkinter directly, the result is not found Tkinter package

> PIP3 Install tkinter

collecting tkinter could not find

a version that satisfies the requirement Tkinter Versions:)

No matching distribution found for Tkinter
what is Tkinter, Tcl, TK

The Tkinter package ("TK interface") is the standard Python interface to the TK GUI Toolkit. Both Tk and Tkinter are available on most Unix platforms, as as is on Windows systems. (Tk itself is not part's Python; it is maintained at ActiveState.) You can check this tkinter is properly installed on your system by running Python-m tkinter to the command line; This is should open a window demonstrating a simple Tk interface.
The above files are excerpted from the Python community: https://docs.python.org/3/library/tkinter.html

Tkinter is in fact Python's standard Python program that invokes TCL programs, which can invoke TCL's program through this interface, because many of the TCL programs and commands are built into most UNIX systems.

TCL is the acronym for the Tool Control language (Tool Command Language), which is object-oriented OTCL language. Tk is an extension of TCL's "graphics Toolbox", which provides a variety of standard GUI interface items to facilitate rapid advanced application development.

So, execute the terminal command:

Python-m Tkinter
If you do the right thing, you can see the following interface directives, mainly in Linux desktop systems:
try to solve the problem-1

After the first search on the network, it was found that PYTHON-TK/PYTHON3-TK's class libraries needed to be installed at the operating system level. This case takes Ubuntu as an example to inquire whether it is installed:

>sudo apt Search python3-tk
is sorting ... Complete
Full-text Search ... Complete
python3-tk/artful,now 3.6.3-0UBUNTU1 AMD64 [installed]
tkinter-writing TK applications with Python 3.x
Python3-tk-dbg/artful 3.6.3-0ubuntu1 amd64
tkinter-writing tk applications with Python 3.x (debug extension)
py Thon3-tksnack/artful,artful,now 2.2.10.20090623-dfsg-6 all [installed]
Sound extension to TCL/TK and Python/tkinter- Python 3.x Library

The above is the display information already installed, if not installed, you need to execute, installation instructions:

sudo apt install python3-tk (Ubuntu)
Yum install PYTHON3-TK (Centos)

After the installation completes, the program is rerun and the same error is reported, and the problem persists.
Then there is information that says that a TK development class library needs to be installed, so that after the query is not installed, it is installed directly:

sudo apt install tk-dev (ubuntu/debian)
Yum install Tk-devel (CentOS)
After installation, the error persists after the program is executed again. try to solve the problem -2

Search on the StackOverflow after a few, found that some people are prompted to say that in some cases, TCL/TK installation is not complete, and then back down the TCL/TK source code package:

Download Link: http://www.tcl.tk/software/tcltk/download.html

Then separate down the source code, after compression, into the source code directory:

Configure
Make or make test
sudo make install
The problem persists after reinstalling the TCK/TK and then executing the code again. finally solve the problem

After trying a few times, I feel that the installation and setup options have been done, how the problem persists. I suddenly noticed the first key message in the error message:

If This fails your Python may is configured for Tk

Perhaps the problem here is not tkinter installed, or properly installed, but not properly configured. The contents of the TCL/TK are now installed, but not in Python, but python3-tk and the like are already installed, but the problem has not been solved. If so, reinstall the Python run environment.

Since the current *unix system is based on the Python 2.x series, if you need to use Python3, you will need to compile and install your own code, then recompile and install it based on the source code, and then go to the Python command line.

Import Tkinter

prompts to load correctly. The original program is then executed again, and the results are correctly printed. So far, the problem has been solved correctly. Process Summary Analysis

Here is a summary of the points of gain and loss:

Tkinter is a Python interface class library that invokes the TCL/TK program, so there is a corresponding class library installation at the operating system level, rather than relying on PIP3 to install the corresponding Python library, for example, PYTHON3-TK

Devel library installation, in the *unix system, in the development, in many cases, it is necessary to Devel class library installation, this is a large probability rule.

In some cases, there may be incomplete installation or some missing class libraries will be installed. This is the time to consider overwriting the installation, such as TCL/TK, Python3 reinstall

Attention to error information analysis and directional guidance. In this case, there are 2 key tips, one that Moulde is not installed or found, and tries to resolve it from the direction of the installation class library. Second, assume that the package is installed, but not properly configured. The author in the solution of the problem, the first choice to solve the idea, in the face of various attempts to fail, only have to turn to the second way of thinking to solve. If it is solved in two directions at the same time, or the problem can be solved faster.

The author has spent about 3-4 hours or so on solving this problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.