0

How can I use Tkinter with Python 3.11.3 that I built from source?

I have another version of Python (3.10.6) that came with my distribution (Pop!_OS) and on that version I can successfully import tkinter.

But when I run Python 3.11.3 that I build from source I get the following messages:

>>> import tkinter

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/python/3.11.3/lib/python3.11/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk

I used the following script builder to build Python 3.11.3 from source.

https://www.build-python-from-source.com/

If you have found a way to install Tkinter so that it can work with the version of Python 3.11.3 that you built from source, please reply with the details.

I have done a lot of searching to find a way to do this, but so far have not been successful.

Obviously Tkinter must be on my system, but, for some reason Python 3.11.3 can not "see" it.

acw1668
  • 40,144
  • 5
  • 22
  • 34
Richard Fuhr
  • 3,195
  • 3
  • 22
  • 14
  • You need to have had the Tk headers present at build time. (You'll also want to make sure you have SQLite headers, SSL headers, etc. if you want a complete standard library. [This list](https://github.com/pyenv/pyenv/wiki#suggested-build-environment) is a good starting point.) – ChrisGPT was on strike Apr 24 '23 at 00:02
  • @Chris Thanks for your reply. How does one have the Tk headers present at build time? What has to be in the build script to accomplish this? – Richard Fuhr Apr 24 '23 at 00:12
  • I've never used that script builder, not sure how it works. But with `pyenv`, you just need to install the system packages before compiling Python from source. I believe the Debian / Ubuntu instructions from the link I provided should work with Pop!_OS. – ChrisGPT was on strike Apr 24 '23 at 00:13
  • You need to install *development packages* of `tk` using the system package manager. – acw1668 Apr 24 '23 at 00:19

1 Answers1

0

You need to install required Tcl/Tk development packages like this.

$ sudo apt install tk-dev

Then the Python build system will enable and build the Tkinter extension. (You need to run the configure and make command again.)

relent95
  • 3,703
  • 1
  • 14
  • 17
  • The step that I had been missing was to invoke sudo apt install tk-dev After doing that, and running configure and make again then the python 3.11.3 that I had built from source was able to Import tkinter So thanks for everyone’s replies. – Richard Fuhr Apr 24 '23 at 17:06