pythonpython-3.xtkinterpip

Installing Tkinter somehow installs a different module


I'm on Linux Ubuntu 22.10.

So I installed Tkinter using this command:

python3 -m pip install tk

and the output was fine:

Defaulting to user installation because normal site-packages is not writeable
Collecting tk
  Using cached tk-0.1.0-py3-none-any.whl.metadata (693 bytes)
Using cached tk-0.1.0-py3-none-any.whl (3.9 kB)
Installing collected packages: tk
Successfully installed tk-0.1.0

so I tried to use the module; I wrote a basic file to display a window:

import tk
window = tk.Tk()
window.mainloop()

and then it threw me this error: AttributeError: module 'tk' has no attribute 'Tk'.

huh, weird. I checked what functions the module had, and apparently, none other than __init__, which is empty!

How do I fix this?

EDIT: info subject to change. This question is a train wreck.


Solution

  • The package you installed is called tk and it is completely unrelated to tkinter (though the similar names are unfortunate); it is a machine learning package also called TensorKit and provides some files in a subdirectory called structure (but indeed, slightly weirdly, nothing in the tk package itself).

    It is quite normal for __init__.py to be empty; this file is mandatory for a (non-namespace) Python package, but it often doesn't need to contain anything at all. See also What is __init__.py for?

    In the meantime, tkinter is missing on some platforms for various reasons. To install tkinter on Ubuntu, apt-get install python3-tk. See also Install tkinter for Python.

    This question is a bit of a train wreck so it's perhaps better if you don't upvote or accept this answer, and let the question be deleted when the time comes.