Skip to content

Install tk-dev and tcl-dev before building python #127

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

Merged
merged 1 commit into from
Jul 19, 2016

Conversation

jstewmon
Copy link

Fixes #122

@jstewmon
Copy link
Author

Two of the Travis jobs failed because of connection errors during the apt-get install... No actual issue with the build.

@yosifkit
Copy link
Member

LGTM

cc @tianon

@tianon
Copy link
Member

tianon commented Jul 19, 2016

A few size comparisons for posterity:

image              before    after
python:3.6         684.1 MB  692 MB
python:3.6-slim    200.5 MB  202.5 MB
python:3.6-alpine  71.57 MB  71.57 MB
python:3.5         685.6 MB  693.5 MB
python:3.5-slim    202 MB    204 MB
python:3.5-alpine  73.04 MB  73.04 MB
python:2.7         674.7 MB  682.5 MB
python:2.7-slim    184.1 MB  186.1 MB
python:2.7-alpine  56.45 MB  56.45 MB
python:2.7-wheezy  528.5 MB  537 MB

@tianon
Copy link
Member

tianon commented Jul 19, 2016

LGTM

@tianon tianon merged commit 94652e7 into docker-library:master Jul 19, 2016
@jstewmon jstewmon deleted the tkinter branch July 19, 2016 17:00
jstewmon pushed a commit to jstewmon/official-images that referenced this pull request Jul 19, 2016
- `python`: `build tkinter module` (docker-library/python#127)
JayH5 added a commit to praekeltfoundation/docker-alpine-python that referenced this pull request Jul 21, 2016
JayH5 added a commit to JayH5/pypy that referenced this pull request Sep 1, 2016
@brando90
Copy link

brando90 commented Oct 6, 2017

is there like some simple command to install tk?

@yosifkit
Copy link
Member

yosifkit commented Oct 6, 2017

@brando90, it is already installed (that is what this and #130 fixed). You just need to get the display hooked into the container.

$ docker run -it --rm python:3.6-alpine3.6 python -m tkinter
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/tkinter/__main__.py", line 7, in <module>
    main()
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 3982, in _test
    root = Tk()
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 2017, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

$ docker run -it --rm -e DISPLAY python:3.6-alpine3.6 python -m tkinter
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/tkinter/__main__.py", line 7, in <module>
    main()
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 3982, in _test
    root = Tk()
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 2017, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"

@platonfloria
Copy link

docker run -it --rm python:3.7.4-alpine python -m tkinter
results in

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.7/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/local/lib/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/local/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: Error loading shared library libtk8.6.so: No such file or directory (needed by /usr/local/lib/python3.7/lib-dynload/_tkinter.cpython-37m-x86_64-linux-gnu.so)

@jstewmon
Copy link
Author

@platonfloria, you need to reinstall the tk package:

$ docker run -i --rm python:3.7.4-alpine sh - <<EOF
apk add -U tk
python -m tkinter
EOF
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/12) Installing libxau (1.0.9-r0)
(2/12) Installing libbsd (0.9.1-r0)
(3/12) Installing libxdmcp (1.1.3-r0)
(4/12) Installing libxcb (1.13.1-r0)
(5/12) Installing libx11 (1.6.8-r1)
(6/12) Installing libxrender (0.9.10-r3)
(7/12) Installing libpng (1.6.37-r1)
(8/12) Installing freetype (2.10.0-r0)
(9/12) Installing fontconfig (2.13.1-r0)
(10/12) Installing libxft (2.3.3-r0)
(11/12) Installing tcl (8.6.9-r0)
(12/12) Installing tk (8.6.9-r0)
Executing busybox-1.30.1-r2.trigger
OK: 34 MiB in 47 packages
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.7/tkinter/__main__.py", line 7, in <module>
    main()
  File "/usr/local/lib/python3.7/tkinter/__init__.py", line 3988, in _test
    root = Tk()
  File "/usr/local/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

The tk package is uninstalled after python is compiled in the docker image to save space, since it is uncommon for image users to need tk.

@rayluo
Copy link

rayluo commented Jan 17, 2021

@jstewmon and/or @platonfloria Have you tried actually setting up DISPLAY etc. to run it? My following run was not successful.

$ docker run -e DISPLAY=$DISPLAY --net=host python:3-alpine sh -c "apk add tk && python -m tkinter"
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/13) Installing libxau (1.0.9-r0)
(2/13) Installing libbsd (0.10.0-r0)
(3/13) Installing libxdmcp (1.1.3-r0)
(4/13) Installing libxcb (1.14-r1)
(5/13) Installing libx11 (1.6.12-r0)
(6/13) Installing libxrender (0.9.10-r3)
(7/13) Installing brotli-libs (1.0.9-r1)
(8/13) Installing libpng (1.6.37-r1)
(9/13) Installing freetype (2.10.4-r0)
(10/13) Installing fontconfig (2.13.1-r2)
(11/13) Installing libxft (2.3.3-r0)
(12/13) Installing tcl (8.6.10-r0)
(13/13) Installing tk (8.6.10-r1)
Executing busybox-1.31.1-r19.trigger
OK: 32 MiB in 49 packages

Here, there should be a demo tkinter window popping up. But it did not. And I got this error.

$ echo $?
139

FWIW, if I use python:3-slim as my base, it would work just fine on my same Linux desktop, so I'd assume it is not something on my host OS to be blamed.

@jstewmon
Copy link
Author

@rayluo, sorry but I never directly used tkinter - I needed support for it to create a build environment for PyInstaller, which imported tkinter.

@rayluo
Copy link

rayluo commented Jan 18, 2021

@jstewmon , interesting. I wonder how your "build environment for PyInstaller" would manage to work. It might pass the build procedure while the tkinter package was technically available, but could your PyInstaller actually fire up a GUI?

Regardless, is the current failing to launch a tkinter gui from docker run -e DISPLAY=$DISPLAY --net=host python:3-alpine sh -c "apk add tk && python -m tkinter" considered a bug? Shall I go ahead to create a new issue in this repo?

UPDATE: Never mind. Happened to find a workaround. Adding a xvfb package does the trick. Full command docker run -e DISPLAY=$DISPLAY --net=host python:3-alpine sh -c "apk add tk xvfb && python -m tkinter".

@yosifkit
Copy link
Member

$ docker run --rm -it -e DISPLAY=$DISPLAY python:3-alpine sh -c 'apk add --no-cache tk && python -m tkinter'
Unable to find image 'python:3-alpine' locally
3-alpine: Pulling from library/python
801bfaa63ef2: Already exists 
8723b2b92bec: Pull complete 
4e07029ccd64: Pull complete 
594990504179: Pull complete 
140d7fec7322: Pull complete 
Digest: sha256:7492c1f615e3651629bd6c61777e9660caa3819cf3561a47d1d526dfeee02cf6
Status: Downloaded newer image for python:3-alpine
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/13) Installing libxau (1.0.9-r0)
(2/13) Installing libbsd (0.10.0-r0)
(3/13) Installing libxdmcp (1.1.3-r0)
(4/13) Installing libxcb (1.14-r1)
(5/13) Installing libx11 (1.6.12-r0)
(6/13) Installing libxrender (0.9.10-r3)
(7/13) Installing brotli-libs (1.0.9-r1)
(8/13) Installing libpng (1.6.37-r1)
(9/13) Installing freetype (2.10.4-r0)
(10/13) Installing fontconfig (2.13.1-r2)
(11/13) Installing libxft (2.3.3-r0)
(12/13) Installing tcl (8.6.10-r0)
(13/13) Installing tk (8.6.10-r1)
Executing busybox-1.31.1-r19.trigger
OK: 32 MiB in 49 packages
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.9/tkinter/__main__.py", line 7, in <module>
    main()
  File "/usr/local/lib/python3.9/tkinter/__init__.py", line 4553, in _test
    root = Tk()
  File "/usr/local/lib/python3.9/tkinter/__init__.py", line 2262, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0"

You probably need the X11 socket: https://stackoverflow.com/a/50338944 and an XAUTHORITY file & variable https://stackoverflow.com/a/25280523.

Since GitHub isn't really a help forum, it'd be better to post future questions like this in the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

@rayluo
Copy link

rayluo commented Jan 18, 2021

@yosifkit , thanks for chiming in! Actually, I've pasted my command line in each of my recent posts here. The use of ... -e DISPLAY=$DISPLAY --net=host ... should have already satisfied the X11 requirement. (You would likely still need an xhost + as prerequisite, though.)

That being said, now I already figured it out (after reading a dozen stackoverflow posts), and left a solution in my previous message. Paste it again here, for future readers. The following line works for me.

xhost + && docker run -e DISPLAY=$DISPLAY --net=host python:3-alpine sh -c "apk add tk xvfb && python -m tkinter"

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

Successfully merging this pull request may close these issues.

python is not configured for tkinter
6 participants