Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyenv install doesn't work with homebrew installed tcl-tk #1375

Closed
11 tasks done
grahamannett opened this issue Jul 18, 2019 · 36 comments
Closed
11 tasks done

pyenv install doesn't work with homebrew installed tcl-tk #1375

grahamannett opened this issue Jul 18, 2019 · 36 comments

Comments

@grahamannett
Copy link
Contributor

grahamannett commented Jul 18, 2019

When installing any version of python with pyenv, I can't get tkinter to work. Python install fine but I believe that the files related to tcl-tk are causing issues and python-build either needs to skip the files related in

For instance with python installed from pyenv in any manner of way:

env CFLAGS="-I/usr/local/opt/tcl-tk/include" CPPFLAGS="-I/usr/local/opt/tcl-tk/include" LDFLAGS="-L/usr/local/opt/tcl-tk/lib"  pyenv install --verbose 3.7.4
env CPPFLAGS="-I/usr/local/opt/tcl-tk/include" LDFLAGS="-L/usr/local/opt/tcl-tk/lib"  PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install --verbose 3.7.4
env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install --verbose 3.7.4

etc.

and then trying to use tkinter from that python:

python -m tkinter -c 'tkinter._test()'                                                                                  
Traceback (most recent call last):
  File "/Users/graham/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/graham/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/graham/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__main__.py", line 7, in <module>
    main()
  File "/Users/graham/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 3988, in _test
    root = Tk()
  File "/Users/graham/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 2025, in __init__
    self._loadtk()
  File "/Users/graham/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 2040, in _loadtk
    % (_tkinter.TK_VERSION, tk_version))
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)

Looking at the log does it matter that the tcl-tk stuff that is included with macosx is included before the brew ones?

building '_tkinter' extension
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/usr/local/opt/tcl-tk/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/usr/local/opt/tcl-tk/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -DWITH_APPINIT=1 -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders -I/System/Library/Frameworks/Tk.framework/Headers -I/System/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders -I/usr/X11R6/include -I./Include -I. -I/usr/local/opt/readline/include -I/usr/local/opt/tcl-tk/include -I/usr/local/include -I/private/var/folders/3f/pc11ycws4wsfbw5sk_7s8cfr0000gn/T/python-build.20190718105125.49297/Python-3.7.4/Include -I/private/var/folders/3f/pc11ycws4wsfbw5sk_7s8cfr0000gn/T/python-build.20190718105125.49297/Python-3.7.4 -c /private/var/folders/3f/pc11ycws4wsfbw5sk_7s8cfr0000gn/T/python-build.20190718105125.49297/Python-3.7.4/Modules/_tkinter.c -o build/temp.macosx-10.14-x86_64-3.7/private/var/folders/3f/pc11ycws4wsfbw5sk_7s8cfr0000gn/T/python-build.20190718105125.49297/Python-3.7.4/Modules/_tkinter.o -framework Tk

For why I think it has to do with pyenv/python-build, the brew installed python 3 works with tkinter /usr/local/bin/python3 -c 'import tkinter; tkinter._test()'

Too many issues will kill our team's development velocity, drastically.
Make sure you have checked all steps below.

Prerequisite

  • Make sure your problem is not listed in the common build problems.
  • Make sure no duplicated issue has already been reported in the pyenv issues. You should look for closed issues, too.
  • Make sure you are not asking us to help solving your specific issue.
    • GitHub issues is opened mainly for development purposes. If you want to ask someone to help solving your problem, go to some community site like Gitter, StackOverflow, etc.
  • Make sure your problem is not derived from packaging (e.g. Homebrew).
    • Please refer to the package documentation for the installation issues, etc.
  • Make sure your problem is not derived from plugins.
    • This repository is maintaining pyenv and the default python-build plugin only. Please refrain from reporting issues of other plugins here.

Description

  • Platform information (e.g. Ubuntu Linux 16.04): mac osx 10.14
  • OS architecture (e.g. amd64):
  • pyenv version: 1.2.13
  • Python version: any
  • C Compiler information (e.g. gcc 7.3):
  • Please attach verbose build log as gist
    • You can turn on verbose debug logging using by setting PYENV_DEBUG=1, e.g. env PYENV_DEBUG=1 pyenv install -v 3.6.4

https://gist.github.com/grahamannett/88a8c06aa864cc38a814046c364537e6

@grahamannett
Copy link
Contributor Author

grahamannett commented Jul 18, 2019

For reference these people seem to have this issue as well:
jiansoung/issues-list#15

This seems like its the same issue as: #1125

@sartorg
Copy link

sartorg commented Jul 19, 2019

In my case, in the verbose log of pyenv I get the following error when trying to build tkinter.

clang: warning: -framework Tk: 'linker' input unused [-Wunused-command-line-argument]
In file included from /private/var/folders/0f/0723vl7935166wxgzrtcm3vw0000gn/T/python-build.20190719020206.17065/Python-2.7.16/Modules/_tkinter.c:71:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/tk.h:78:11: fatal error: 'X11/Xlib.h' file not found
#       include <X11/Xlib.h>
                ^~~~~~~~~~~~
1 error generated.

Interesting thing is: it works without problem with python 3.5.7, but not with 2.7.16. In fact, in both cases I uninstalled the corresponding python and reinstalled it after brew install tcl-tk. With python3 it worked, but not with python2.7.16.

@sartorg
Copy link

sartorg commented Jul 19, 2019

Ok, a comment on my previous comment. I tried to pyenv install 3.7.4 out of curiosity, and it still fails, generating the same error. So, I am not sure what happened in the mean time and why it worked on my 3.5.7 installation. It was a couple of months ago when I did it for the 3.5.7 with success.

@sartorg
Copy link

sartorg commented Jul 19, 2019

Ok, the problem was actually quite clear from my error log. pyenv looks for tk inside the macos sdk, while it should look into the brew installation, if existent, pretty much like it works with zlib.
I believe this should be fixed in pyenv.
A temporary fix is to run:
CFLAGS="-I$(brew --prefix tcl-tk)/include" pyenv install 2.7.16

@grahamannett
Copy link
Contributor Author

@sartorg I think I had tried that and it didn't work for me. I dont really want to try it again since I ended up editing my python-build file to just have the tcl-tk stuff hard coded.

@sartorg
Copy link

sartorg commented Jul 19, 2019

@grahamannett Well, if it is just out of curiosity, you can pyenv install another random python version (like 3.7.3) and delete if afterwards.
If you are willing to do that, could you post the verbose log of the lines when it tries to compile Tk?
Also, what brew --prefix tcl-tk returns for you?
In any case, I am happy you solved it!

@illegalnumbers
Copy link

I am also seeing this.

@tcarroll2
Copy link

When installing python 3.7.4 using pyenv, why doesn't pyenv use tcl-tk from homebrew like it does for openssl and readline? There should be a way to force pyenv to check for a brew installation of tcl-tk and if it exists then use it.

@tcarroll2
Copy link

tcarroll2 commented Aug 23, 2019

@grahamannett Well, if it is just out of curiosity, you can pyenv install another random python version (like 3.7.3) and delete if afterwards.
If you are willing to do that, could you post the verbose log of the lines when it tries to compile Tk?
Also, what brew --prefix tcl-tk returns for you?
In any case, I am happy you solved it!

I just ran this and I get /usr/local/opt/tcl-tk

I'm going to try your suggested CFLAGS and see if that works on my 10.14.6 system.

@tcarroll2
Copy link

tcarroll2 commented Aug 23, 2019

Ok, the problem was actually quite clear from my error log. pyenv looks for tk inside the macos sdk, while it should look into the brew installation, if existent, pretty much like it works with zlib.
I believe this should be fixed in pyenv.
A temporary fix is to run:
CFLAGS="-I$(brew --prefix tcl-tk)/include" pyenv install 2.7.16

@sartorg I tried installing like this (using install 3.7.4) and I still get
iMac:~ carroll$ python -m tkinter -c 'tkinter._test()
Traceback (most recent call last):
File "/Users/carroll/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "/Users/carroll/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/carroll/.pyenv/versions/3.7.4/lib/python3.7/tkinter/main.py", line 7, in
main()
File "/Users/carroll/.pyenv/versions/3.7.4/lib/python3.7/tkinter/init.py", line 3988, in _test
root = Tk()
File "/Users/carroll/.pyenv/versions/3.7.4/lib/python3.7/tkinter/init.py", line 2025, in init
self._loadtk()
File "/Users/carroll/.pyenv/versions/3.7.4/lib/python3.7/tkinter/init.py", line 2040, in _loadtk
% (_tkinter.TK_VERSION, tk_version))
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)

My log file of the install of 3.7.4 shows the brew installation of tcl-tk is being pulled in.
pyenv.log

This is becoming maddening!

@tcarroll2
Copy link

tcarroll2 commented Aug 23, 2019

Finally! After all of this, I got it to install! Whew! Here's what I did so it may help others:

PyEnv Python installation steps to get the latest tcl/tk version on Mac OS 10.14.6 (Mojave)

UPDATED for Mac OS Big Sur on Apple Silicon

  1. install pyenv via homebrew
    $brew install pyenv

NOTE: As of this update Homebrew will install the Arm version of pyenv in /opt/homebrew/pyenv.

  1. install tcl-tk via home brew
    $brew install tcl-tk

NOTE: As of this update Homebrew will installs the Arm version of tcl-tk in /opt/homebrew/tcl-tk.

  1. to get tcl-tk 8.6 to work with the pyenv install of python, you must
    edit the python-build script file. It may be located in one these two locations:

    ~/.pyenv/plugins/python-build/bin/python-build

or:

/usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build

**NOTE: For the Arm version you will need to edit /opt/homebrew/Cellar/pyenv/1.2.22/plugins/python-build/bin/python-build **

Once you have the script file open, search for:
$CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
and replace with:
$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' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

NOTE: For the Arm build it will have to read $CONFIGURE_OPTS --with-tcltk-includes='-I/opt/homebrew/Cellar/tcl-tk/8.6.11/include' --with-tcltk-libs='-L/opt/homebrew/Cellar/tcl-tk/8.6.11/lib -ltcl8.6 -ltk8.6' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

  1. install Python 3.7.4 in pyenv
    $pyenv install 3.7.4

NOTE: For Arm version $pyenv install 3.9.1

  1. set the default python version via the global variable and check it
    $pyenv global 3.7.4 or $pyenv global 3.9.1
    $pyenv version

  2. Add to your .bash_profile to set the pyenv every time you load your shell
    $echo -e 'if command -v pyenv 1&gt;/dev/null 2&gt;&amp;1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

  3. reset your shell
    $exec "$SHELL"

  4. check your environment
    $which python
    $python -V
    $which pip
    $pip -V

    (check if your pip needs upgrading with $pip install --upgrade pip)

  5. Test your tcl-tk installation with $python -m tkinter -c 'tkinter._test()'

If you don't get any errors when running the above test, your installation should work

Credits:

https://opensource.com/article/19/5/python-3-default-mac

#1125

lantrix added a commit to lantrix/Trade-Dangerous that referenced this issue Sep 3, 2019
Document fix for Mac users who use pyenv Python installation, to get the latest
tcl/tk version working on Mac OS 10.14.6 (Mojave).
eyeonus pushed a commit to eyeonus/Trade-Dangerous that referenced this issue Sep 3, 2019
Document fix for Mac users who use pyenv Python installation, to get the latest
tcl/tk version working on Mac OS 10.14.6 (Mojave).
@grahamannett
Copy link
Contributor Author

this should be fixed with #1397. when installing I was using/testing with
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'" pyenv install 3.7.4

@gitfourteen
Copy link

gitfourteen commented Nov 5, 2019

Finally succeed with this method!!!

System info

macOS Catalina 10.15.1, tcl-tk 8.6.9, pyenv 1.2.15, and python 3.7.5.

Some Notes for your reference:

  1. DO NOT brew install python before Step 4 install python in pyenv, as this MacOS homebrew python 3.7.x with tcl-tk did. If installed, uninstall it.

  2. the right path of the python-build should be:
    /usr/local/Cellar/pyenv/1.2.15/plugins/python-build/bin/python-build

  3. Better insert the additional arguments to the original python-build to keep in one line when editing with vim/vi...
    --with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'

  4. if you need to use PyInstaller, which require CPython installation built, the step 4 should be
    $ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.x

  5. Not necessary to set 'pyenv global 3.7.x', can set pyenv local 3.7.x for target directory

  6. in this target directory further use pipenv install to get a virtual env with python 3.7.x, which is OK for tkinter

@grahamannett
Copy link
Contributor Author

grahamannett commented Jan 13, 2020

If people are still seeing this and having issues, theres a better PR made that will automatically check for tcl-tk homebrew installed #1409

I don't know what the similar way to do this for linux related installs would be but hopefully it's merged at some point? If someone can explain how to create and test this issue in a linux distro, I would be happy to look into.

Also if someone has an idea for how to test this (as the only way I have been able to reliably tell is something along the lines of python -m tkinter -c 'tkinter._test()' and verifying the popup box), please let me know!

@zjhmale
Copy link

zjhmale commented Jan 18, 2020

@grahamannett works for me, thx!

@sri0sharma
Copy link

@grahamannett, thanks much for your post. I spent quite a bit of time, failing and failing again, and then I finally succeeded. For others who are novice like me, I am listing the steps I took to successfully install tcl-tk and IDLE to work in MacOS Catalina (10.15.3). It's mostly yours, with a few additional in between.:

  1. ran $brew install pyenv
    Output: Warning: pyenv 1.2.16 is already installed and up-to-date
    To reinstall 1.2.16, ran brew reinstall pyenv
    /usr/local/Cellar/pyenv/1.2.16: 671 files, 2.5MB

  2. ran $brew install tcl-tk

Output: tcl-tk 8.6.10 is already installed and up-to-date
To reinstall 8.6.10, run brew reinstall tcl-tk

Output after brew reinstall tcl-tk:
tcl-tk is keg-only, which means it was not symlinked into /usr/local,
because tk installs some X11 headers and macOS provides an (older) Tcl/Tk.

If you need to have tcl-tk first in your PATH run:
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
(I added it to ~/.zshrc as the first line)

For compilers to find tcl-tk you may need to set:
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
(I added these two to ~/.zshrc, too, after adding the above)

For pkg-config to find tcl-tk you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
(I added this to ~/.zshrc, too, after adding the above)

  1. To get tcl-tk 8.6 to work with the pyenv install of python, I found:
    /usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build
    and replaced the following:
    $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
    with:
    $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' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

  2. ran $pyenv install 3.8.1
    Installed Python-3.8.1 to /Users/ryan/.pyenv/versions/3.8.1

  3. ran $pyenv global 3.8.1
    Refreshed the current terminal and checked by running

  4. $pyenv version
    output: 3.8.1 (set by /Users/ryan/.pyenv/version)

Ran $python -V
output: Python 3.8.1

  1. ran $pip install --upgrade pip (since I had previously already installed pip using $pip install)
    output: Successfully installed pip-20.0.2

  2. Tested my tcl-tk installation with $python -m tkinter -c 'tkinter._test()'
    Output: Tk window popped up. Hit ‘Quit’ to back to Terminal.

  3. Ran $ idle
    Output: Python 3.8.1 Shell window popped up.

@ivanoats
Copy link

I made all the modifications in the last comment, thank you @sri0sharma ! but I'm still getting a version mismatch.. any tips?

DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__main__.py", line 7, in <module>
    main()
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 4552, in _test
    root = Tk()
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2263, in __init__
    self._loadtk()
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2279, in _loadtk
    raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)

@sri0sharma
Copy link

Hi @ivanoats, I am not an expert. I got my python and tck running by trial and error. I campared your files "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/main.py", and "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/init.py"
with mine, and they are exactly the same on the respective lines. The only thing which comes to my mind is the following:

  1. if the step 3 was properly executed:
    To get tcl-tk 8.6 to work with the pyenv install of python, you must
    edit the python-build script file. It may be located in one these two locations (I may have gotten this from https://stackoverflow.com/questions/28921333/installed-python-3-on-mac-os-x-but-its-still-python-2-7):
    ~/.pyenv/plugins/python-build/bin/python-build
    or:
    /usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build
    Once you have the script file open, search for:
    $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
    and replace with:
    $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' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

  2. if your ~/.zshrc file is OK? By the way, did you add the following to your ~/.zshrc for python version to work ok:
    if command -v pyenv 1>/dev/null 2>&1; then
    eval "$(pyenv init -)"
    fi
    These three are the last three lines in my ~/.zshrc for python version to come out correctly. I do not know if it will resolve your tk.h version problem.

I don't exactly recall, but I had to try a few times, before my steps worked. In fact, I did uninstall my brew and pyenv and did clean install again for these steps to work. Good luck!

@natersoz
Copy link

This issue was closed, but how was it resolved?
I see lots of disparate things that people have done - but which one works?

@sri0sharma
Copy link

sri0sharma commented Apr 24, 2020 via email

@jlpearso
Copy link

jlpearso commented Jun 4, 2020

Similar to others, this link (https://stackoverflow.com/questions/60469202/unable-to-install-tkinter-with-pyenv-pythons-on-macos/60469203#60469203) fixed everything, and I did not have to manually deal with the build files etc. It even has a comment about the error that gets thrown for 8.5 versus 8.6. Worked perfectly for me, hope it helps you out too.

@sri0sharma
Copy link

sri0sharma commented Jun 4, 2020 via email

@jlpearso
Copy link

jlpearso commented Jun 4, 2020 via email

@ryanleonbutler
Copy link

  1. $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' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

This method worked for me.

@rritoch
Copy link

rritoch commented Jul 2, 2020

@grahamannett, thanks much for your post. I spent quite a bit of time, failing and failing again, and then I finally succeeded. For others who are novice like me, I am listing the steps I took to successfully install tcl-tk and IDLE to work in MacOS Catalina (10.15.3). It's mostly yours, with a few additional in between.:

1. ran $`brew install pyenv`
   Output: Warning: pyenv 1.2.16 is already installed and up-to-date
   To reinstall 1.2.16, ran `brew reinstall pyenv`
   /usr/local/Cellar/pyenv/1.2.16: 671 files, 2.5MB

2. ran $`brew install tcl-tk`

Output: tcl-tk 8.6.10 is already installed and up-to-date
To reinstall 8.6.10, run brew reinstall tcl-tk

Output after brew reinstall tcl-tk:
tcl-tk is keg-only, which means it was not symlinked into /usr/local,
because tk installs some X11 headers and macOS provides an (older) Tcl/Tk.

If you need to have tcl-tk first in your PATH run:
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
(I added it to ~/.zshrc as the first line)

For compilers to find tcl-tk you may need to set:
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
(I added these two to ~/.zshrc, too, after adding the above)

For pkg-config to find tcl-tk you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
(I added this to ~/.zshrc, too, after adding the above)

1. To get tcl-tk 8.6 to work with the pyenv install of python, I found:
   /usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build
   and replaced the following:
   $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
   with:
   $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' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

2. ran $`pyenv install 3.8.1`
   Installed Python-3.8.1 to /Users/ryan/.pyenv/versions/3.8.1

3. ran $`pyenv global 3.8.1`
   Refreshed the current terminal and checked by running

4. $`pyenv version`
   output: 3.8.1 (set by /Users/ryan/.pyenv/version)

Ran $python -V
output: Python 3.8.1

1. ran $`pip install --upgrade` pip (since I had previously already installed pip using $`pip install`)
   output: Successfully installed pip-20.0.2

2. Tested my tcl-tk installation with $`python -m tkinter -c 'tkinter._test()'`
   Output: Tk window popped up. Hit ‘Quit’ to back to Terminal.

3. Ran $ `idle`
   Output: Python 3.8.1 Shell window popped up.

This worked for me with a few small adjustments.

Instead of ~/.zshrc I had to put the export statements into ~/.profile because I'm using bash. The version of pyenv has changed so the path to the pyenv python-build file changed and 1.2.13 needed to be replaced with my version. Lastly I uninstalled the previous version I had installed withpyenv uninstall 3.5.8 and then re-installed.

This solution also works with pipenv which uses pyenv to install python.

@sri0sharma
Copy link

sri0sharma commented Jul 4, 2020 via email

@elgow
Copy link

elgow commented Jul 19, 2020

The easier way to do this w/o editing homebrew files is provided by the PR described here and the command line to do it is e.g.

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'" pyenv install 3.8.3

@davedgd
Copy link

davedgd commented Aug 12, 2020

Building on @elgow's comment, this also worked for me with no editing of pyenv or .zshrc (including --enable-shared or --enable-framework), albeit with a slight adjustment (mainly adding env to the front of the line):

env 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' --enable-shared" pyenv install 3.8.5

@CathyBing
Copy link

I ran into the same problem and I tried everything I can, but it still can't find where the _tkinter module is
Finially, this works:
brew uninstall pyenv
brew install python3
Then, tkinter is perfect to use. Goodbye pyenv.

@MagicInsider
Copy link

My nickel into the tkinter on OS X community stash:
As you value your life or your reason, keep away from the pyenv.
Go conda.

@louisabraham
Copy link

I have the same issue on arch: why isn't tkinter compiled with python?

@cverrier
Copy link

cverrier commented May 20, 2021

Thanks a lot for the solution! For people who are following this tutorial, be sure to change to your current pyenv version in the line

/usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build

if it is your path for the python-build script.

@sri0sharma
Copy link

sri0sharma commented May 23, 2021 via email

@cobenash
Copy link

I made all the modifications in the last comment, thank you @sri0sharma ! but I'm still getting a version mismatch.. any tips?

DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__main__.py", line 7, in <module>
    main()
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 4552, in _test
    root = Tk()
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2263, in __init__
    self._loadtk()
  File "/Users/ivan/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2279, in _loadtk
    raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)

I've faced the same situation and I finally figured out that I didn't install virtualenv.

After I installed the plugin virtualenv and reinstall the python via pyenv install 3.x.x, everything works as expected.

@xshapira
Copy link

xshapira commented Jun 3, 2023

I was able to fix it by following these two steps.

Step 1: Installing Necessary System Packages

Using tkinter with pyenv can be tricky due to missing dependencies.

First, let's make sure we have installed the necessary system packages for tkinter.

In most Linux systems, you can install them with the following commands:

sudo apt-get update
sudo apt-get install tk-dev

On macOS, you can use Homebrew to achieve the same:

brew install tcl-tk

Step 2: Linking the Correct Tcl/Tk Versions

Next, make sure you link the correct versions of Tcl/Tk when installing Python. pyenv builds Python in your environment, but if you don't have the required dependencies, like the Tk/Tcl libraries, it'll build Python without them.

If the Python version you want to use is already installed on your system, you'll need to uninstall it before proceeding. For example:

pyenv uninstall 3.11.3

When installing a new Python version with pyenv, use the following commands:

For Linux:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install <version>

For macOS, after installing tcl-tk with brew:

brew install openssl readline sqlite3 xz zlib

env LDFLAGS="-L$(brew --prefix openssl@1.1)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix sqlite3)/lib -L$(brew --prefix xz)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix tcl-tk)/lib" \
CPPFLAGS="-I$(brew --prefix openssl@1.1)/include -I$(brew --prefix readline)/include -I$(brew --prefix sqlite3)/include -I$(brew --prefix xz)/include -I$(brew --prefix zlib)/include -I$(brew --prefix tcl-tk)/include" \
PKG_CONFIG_PATH="$(brew --prefix openssl@1.1)/lib/pkgconfig:$(brew --prefix readline)/lib/pkgconfig:$(brew --prefix sqlite3)/lib/pkgconfig:$(brew --prefix xz)/lib/pkgconfig:$(brew --prefix zlib)/lib/pkgconfig:$(brew --prefix tcl-tk)/lib/pkgconfig" \
pyenv install <version>

Replace <version> with the version of Python you want to install. After that, you should be able to import tkinter in your pyenv Python environment.

Note that I couldn't get tkinter to work with pyenv on Python 3.11.0, but it worked perfectly on 3.11.2.

@Zwiqler94
Copy link

Installing tcl-tk and python
/ following tkdocs works:
PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/Library/Frameworks/Tcl.framework/Headers|-I/Library/Frameworks/Tk.framework/Headers' --with-tcltk-libs='/Library/Frameworks/Tcl.framework/Tcl|/Library/Frameworks/Tk.framework/Tk'" pyenv install 3.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests