1

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?

frie
  • 47
  • 5
  • Lookup where the PATH is, environment etc. Type `echo PATH` in terminal to see where it is currently set. – m_krsic Mar 15 '23 at 13:55
  • @steeldriver thank you for the comment. I have edited my question – frie Mar 15 '23 at 14:39
  • @m_krsic when it finds 'python3.10' it should find tkinter because it is in a subdirectory – frie Mar 15 '23 at 14:58
  • @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` – steeldriver Mar 15 '23 at 16:26
  • @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' – frie Mar 15 '23 at 16:31
  • 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 – steeldriver Mar 15 '23 at 16:42
  • 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? – frie Mar 15 '23 at 16:51
  • So can you see a `_tkinter.cpython-310-xxxxxx.so` file in `/usr/lib/python3.10/lib-dynload/` ? – steeldriver Mar 15 '23 at 16:57
  • @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 Mar 16 '23 at 09:11
  • @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.....` – frie Mar 16 '23 at 09:20
  • 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? – steeldriver Mar 16 '23 at 11:29
  • @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. – frie Mar 16 '23 at 11:37

1 Answers1

0

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?

frie
  • 47
  • 5
  • 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? – steeldriver Mar 16 '23 at 13:40
  • @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. – frie Mar 16 '23 at 13:51
  • 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. – steeldriver Mar 16 '23 at 13:56
  • @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. – frie Mar 16 '23 at 14:39