6

So I'm stumped. How do I get python 3.7.x working with tkinter with asdf?

I did the following:

1) asdf local python 3.7.4

2) brew install tcl-tk

3) brew link tcl-tk --force

4) python -m venv --system-site-packages nltk

I have some code like:

import nltk
from nltk.corpus import wordnet as wn
from tkinter import *

# Let's get the first sense of vehicle
vehicle = wn.synsets('vehicle')[0]
# Let's build a concept tree
t = nltk.Tree(vehicle.name(), children=[
    nltk.Tree(vehicle.hyponyms()[3].name(), children=[]),
    nltk.Tree(vehicle.hyponyms()[4].name(), children=[]),
    nltk.Tree(vehicle.hyponyms()[5].name(), children=[]),
    nltk.Tree(vehicle.hyponyms()[7].name(), children=[
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[1].name(), children=[]),
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[3].name(), children=[]),
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[4].name(), children=[]),
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[5].name(), children=[]), nltk.Tree(vehicle.hyponyms()[7].hyponyms()[6].name(), children=[]),
        ]),
    ])
t.draw()

Then I run the python script containing code above using the nltk library to draw a concept tree. I get the following output:

Traceback (most recent call last):
  File "concept_tree.py", line 3, in <module>
    from tkinter import *
  File "/Users/alexander/.asdf/installs/python/3.7.4/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'
Nona
  • 5,302
  • 7
  • 41
  • 79

1 Answers1

6

asdf python uses pyenv under the hood, so you can use all the same build options.

In pyenv, you would need to prepend the install command with the following in order to install with tkinter:

PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"

This can also be prepended to asdf install python 3.7.4.

PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'" asdf install python 3.7.4

Note that this needs to be done on a fresh install of python. If you already have python installed, you will need to first uninstall & reshim before running the command with the configure opts.

Katherine
  • 2,086
  • 1
  • 14
  • 23