Score:1

Can not use tkinter anymore and when I try to install the system tells me that I have already the latest version

in flag

I have just upgraded from 20.04 to 22.04. Now I can not use tkinter anymore.

When I start the Python interpreter in the terminal with 'Python3.10' and then typing 'import tkinter' the system tells me: No module named '_tkinter'.

When I try to install with 'sudo apt-get -y install python3-tk' the system tells me: python3-tk is already the newest version (3.10.6-1~22.04).

I have searched this site and almost the complete internet, but nothing seems to work.

How do I get tkinter active again?

m_krsic avatar
de flag
Lookup where the PATH is, environment etc. Type `echo PATH` in terminal to see where it is currently set.
frie avatar
in flag
@steeldriver thank you for the comment. I have edited my question
frie avatar
in flag
@m_krsic when it finds 'python3.10' it should find tkinter because it is in a subdirectory
hr flag
@frie thanks - so is it possible that some of the package's files got manually deleted? If so, try *reinstalling* rather than simply installing i.e. `sudo apt install --reinstall python3-tk`
frie avatar
in flag
@steeldriver I have tried your suggestion but it did not make a difference. I still get the message 'import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'
hr flag
OK so I can induce that exact error text (which would be useful to add to your question imho) by removing `/usr/lib/python3.10/lib-dynload` from sys.path so I suspect there is something screwed up in the initialization of your PYTHONPATH
frie avatar
in flag
Thank you @steeldriver, but isn't it strange than that the module 'sqlite3' is found without any problem while it is in the same directory?
hr flag
So can you see a `_tkinter.cpython-310-xxxxxx.so` file in `/usr/lib/python3.10/lib-dynload/` ?
frie avatar
in flag
@steeldriver Yes I can see that file: _tkinter.cpython-310-x86_64-linux-gnu.so in /usr/lib/python3.10/lib-dynload$. I am now reading in the Python tutorial about sys.path and pythonpath. Is it possible that these have something to do with my problem?
frie avatar
in flag
@steeldriver Now I see that `/usr/lib/python3.10/lib-dynload/` is not in the sys.path. All directories in it start with`/usr/local/lib/.....` And in directory `/usr/local/lib/python3.10/lib-dynload$` I can see NO entry with `_tkinter.....`
hr flag
AFAIK apt would not place files in /usr/local, so are you using the *default* python3.10 from the Ubuntu 22.04 repo, or have you manually installed python3.10 outside of that? Is a PYTHONPATH variable set in your environment?
frie avatar
in flag
@steeldriver I am using the default python3.10 from the ubuntu 22.04 repo. The error showed up directly after upgrading from 20.04 to 22.04. `echo $PYTHONPATH` displays an empty line.
Score:0
in flag

Exploring the problem after the comments of @steeldriver and @_mkrsic I was able to prevent or work around the problem with the creation of an extra line in the .bashrc file: export PYTHONPATH="${PYTHONPATH}:/usr/lib/python3.10/lib-dynload/". There is however a question left for me: Is this the correct way, because why was the line about Tkinter missing in the /usr/local/lib/python3.10/lib-dynload/ file?

hr flag
If it works, it's "correct" (that's what `PYTHONPATH` is provided for) however it shouldn't be necessary - why are you executing `python3.10` explicitly, rather than plain `python3`? Does `type -a python3.10` show a path in `/usr` or in `/usr/local`, or both?
frie avatar
in flag
@steeldriver `type -a python3.10` shows a path in both. I also realize now that I don't have a specific reason for executing `python3.10`, I suppose it is simply a bad habit and besides that `python3` works as well.
hr flag
OK so you **have** installed a version of python3.10 manually in `/usr/local`, and that's likely the version you get (along with its packages) when you invoke `python3.10`. OTOH when you install python3-tk with `apt`, **its** packages go in `/usr` rather than `/usr/local` and hence are not available.
frie avatar
in flag
@steeldriver NO I never installed a python version manually. I have always used the version that came with the standard ubuntu installs. In ubuntu 20.04 I once have installed Spyder. It is possible that that application brought some extra's.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.