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
grahamannett opened this issue Jul 18, 2019 · 13 comments
Closed

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

grahamannett opened this issue Jul 18, 2019 · 13 comments

Comments

@grahamannett
Copy link
Contributor

@grahamannett 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

This comment has been minimized.

Copy link
Contributor Author

@grahamannett 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

This comment has been minimized.

Copy link

@sartorg 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

This comment has been minimized.

Copy link

@sartorg 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

This comment has been minimized.

Copy link

@sartorg 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

This comment has been minimized.

Copy link
Contributor Author

@grahamannett grahamannett commented Jul 19, 2019

@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

This comment has been minimized.

Copy link

@sartorg 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

This comment has been minimized.

Copy link

@illegalnumbers illegalnumbers commented Aug 15, 2019

I am also seeing this.

@tcarroll2

This comment has been minimized.

Copy link

@tcarroll2 tcarroll2 commented Aug 22, 2019

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

This comment has been minimized.

Copy link

@tcarroll2 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

This comment has been minimized.

Copy link

@tcarroll2 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

This comment has been minimized.

Copy link

@tcarroll2 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)

  1. install pyenv via homebrew
    $brew install pyenv

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

  3. 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

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

  1. install Python 3.7.4 in pyenv
    $pyenv install 3.7.4

  2. set the default python version via the global variable and check it
    $pyenv global 3.7.4
    $pyenv version

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

  4. reset your shell
    $exec "$SHELL"

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

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

  6. 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 added 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

This comment has been minimized.

Copy link
Contributor Author

@grahamannett grahamannett commented Sep 19, 2019

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

This comment has been minimized.

Copy link

@gitfourteen 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.