Python3 installation with tkinter


(Michael Noronha) #1

I’m trying to use a package that depends on tkinter in python, and can’t get it to work. I run into the error “_tkinter” not found, and a suggestion my installation might not have been configured to use tkinter. This stackoverflow post suggested I should use the flags --with-tcl-tk, but I see that was removed from the formula. Is this supposed to just work out of the box? Do I need to use special flags?


(Sean Molenaar) #2

Yeah, tcl-tk went from an option to the default setting.


(Michael Noronha) #3

Hm, it’s possible I’m doing something wrong, rather than having discovered a bug. This is what I see:

[11:14:09] mtn:bin $ ./python3.7
Python 3.7.1 (default, Dec 13 2018, 11:10:43) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
>>> 

This is on a base installation of python from brew with no flags.

Also, I’m not sure if this is helpful, but

[11:17:03] mtn:tkinter $ brew install tcl-tk
Warning: tcl-tk 8.6.8 is already installed and up-to-date

(Sean Molenaar) #4

What do brew doctor and brew upgrade python3 say?


(Michael Noronha) #5

Brew doctor includes quite a lot of things. Here’s what I think is relevant (not dumping the whole file unless it’s needed since I’m not sure if there could be anything sensitive):

e[33mWarning:e[0m Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  ...
  /usr/local/lib/libtcl8.6.dylib
  /usr/local/lib/libtk8.6.dylib

...

e[33mWarning:e[0m Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

...
/usr/local/include/tcl.h
/usr/local/include/tk.h
...

e[33mWarning:e[0m Unbrewed .pc files were found in /usr/local/lib/pkgconfig.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .pc files:
  /usr/local/lib/pkgconfig/tcl.pc
  /usr/local/lib/pkgconfig/tk.pc

I believe that’s it. brew upgrade python3 just reports that python3 is up-to-date.

Thanks so much for looking into this, by the way.


(Sean Molenaar) #6

it seems like there’s also a tcl-tk install that isn’t managed by homebrew? Could you try moving the files it complains about somewhere else and reinstalling both python and tcl-tk?


(Michael Noronha) #7

That was exactly the problem. I got rid of the conflicting files and everything works as expected now. Thanks!


(Ignat Remizov) #8

Hello, it seems like this installs an outdated Tcl/TK for tkinter

ignat$ python3
Python 3.7.2 (default, Jan 13 2019, 12:50:15) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.TclVersion
8.5
>>> tkinter.TkVersion
8.5
>>>exit()
ignat$ brew install tcl-tk
Warning: tcl-tk 8.6.9 is already installed and up-to-date
To reinstall 8.6.9, run `brew reinstall tcl-tk`

Is there a way to tell the python install to use homebrew’s Tcl/TK and not the system’s?


(Bryan Hiebert) #9

After running brew doctor and moving all the tcl/tk files to a different location then:

brew uninstall python3

and

brew install python3

and I was back up and running again with the tkinter library.

sweet! Thank you!