(Linux) In the following program when the command python permissions.ph is issued I simply get another prompt, but when python3 permissions.py is issued I get the error below. Why does python2 see tkinter but python 3 doesn't?

garrett@bedroom ~/Projects/TestArea $ python permissions.py 
garrett@bedroom ~/Projects/TestArea $ python3 permissions.py 
Traceback (most recent call last):
  File "permissions.py", line 1, in <module>
    from Tkinter import *
ImportError: No module named 'Tkinter'
garrett@bedroom ~/Projects/TestArea $ 






from Tkinter import *
def fileopenbox(msg=None, title=None, default=None):
    root = Tk()
    root.withdraw()
    f = tkFileDialog.askopenfilename(parent=root,title=title, initialfile=default)
    if not f: return None
    return os.path.normpath(f)

Dani AI

Generated

Short answer: Python 2 used capitalized module names (Tkinter, tkFileDialog); Python 3 uses lowercase (tkinter, tkinter.filedialog). If changing the import to lowercase did not fix the ImportError, the most common causes are that the Python 3 interpreter you ran does not have the Tk bindings installed, or a local file is shadowing the standard library.

Quick checklist and concrete checks:

  • Test whether tkinter is available: run python3 -m tkinter. If it opens a tiny window, the bindings exist; an ImportError means they are missing.
  • See where the module would come from: python3 -c "import tkinter; print(tkinter.__file__)" (this prints a path or raises ImportError).
  • Install the system package that provides Tk for Python 3 (on Debian/Ubuntu it is python3-tk; use your distro/package manager for the equivalent).
  • Look for name collisions in the working directory: remove any Tkinter.py, tkinter.py, tk.py, stale .pyc or __pycache__ entries that could shadow the real module.
  • If you use a virtualenv or an alternative Python build (Homebrew, pyenv), ensure that build was compiled/packaged with Tcl/Tk support.

For code style and migration: prefer explicit imports (for example, import the tkinter package and its filedialog submodule) and avoid wildcard imports. The cross-version try/except approach suggested by is a good pattern for code that must run under both 2.x and 3.x.

Recommended Answers

All 4 Replies

But I set same error when I change the line from Tkinter import * to from tkinter import *

And change to the tkinter.filedialog?

try:
    # for Python2
    import Tkinter as tk
    import tkFileDialog as tkfd
except ImportError:
    # for Python3
    import tkinter as tk
    import tkinter.filedialog as tkfd
commented: good help +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.