Skip to content

python is not configured for tkinter #122

Description

@jstewmon

Is tkinter purposefully omitted? It seems like the only way to enable it is to rebuild from source.

Adding apt-get update && apt-get install -y --no-install-recommends tk-dev && rm -r /var/lib/apt/lists/* to the current Dockerfiles appears to resolve the issue, but adds ~20MB to the resulting image.

Current behavior:

$ docker run --rm -it python:3.5 python -m tkinter
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/runpy.py", line 174, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.5/runpy.py", line 133, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/local/lib/python3.5/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'

$ docker run --rm -it python:2.7 python -m Tkinter
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/local/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

Activity

  1. jstewmon commented on Jul 15, 2016

    @jstewmon
    Author

    @tianon @yosifkit any thoughts on this? I can submit a PR if there's no objection to adding tk to the official images. I think the -dev package is required for the build, and the lib is required for the runtime.

    My plan to fix would be to install tk and tk-dev from the apt/apk, removing tk-dev after the building python.

  2. kevin-brown commented on Jul 15, 2016

    @kevin-brown

    99% of Docker files shouldn't need the Tkinter files, as Docker isn't designed to handle GUIs (I believe, correct me if I'm wrong).

    You should be able to install Tkinter in a separate Dockerfile that extends the Python ones provided here.

    FROM python:3.5
    
    RUN apt-get update && apt-get install tk-dev && rm -r /var/lib/apt/lists/*
    
  3. jstewmon commented on Jul 15, 2016

    @jstewmon
    Author

    @kevin-brown the tkinter module is defined CPython and is complied into python.

    https://wiki.python.org/moin/TkInter

    I'm not aware of any other way to build/install it as an so.

  4. yosifkit commented on Jul 15, 2016

    @yosifkit
    Member

    Since this must be complied into python and cannot be added later, I propose a possible solution to emphasize the "include everything" default tag vs the tiny slim and alpine tags:

    1. Install tk-dev tk tcl-dev tcl during install so that tkinter is compiled in to python.
      1. remove tk-dev tcl-dev in the non-slim and non-alpine variants
      2. remove tk-dev tk tcl-dev tcl in slim and alpine to save space
        • users of these two varints that want to use tkinter can apt-get update && apt-get install tk tcl (or appropriate alpine apk add)

        • I tested this on the 2.7-slim image and it worked fine

          $ docker run --rm -it python:2.7-slim python -m Tkinter
          ...
          ImportError: libtk8.6.so: cannot open shared object file: No such file or directory
          $ docker run --rm -it python:2.7-slim bash -c 'apt-get update && apt-get install -y tcl tk && python -m Tkinter'
          ...
          _tkinter.TclError: no display name and no $DISPLAY environment variable

    Size differences:

    # current
    python              2.7.12-slim         840fa73abd8a        7 days ago          184.1 MB
    python              2.7.12              774abfa592f5        7 days ago          665.2 MB
    # new
    python              2.7-slim            29cb1e9fce53        3 seconds ago       186.1 MB
    python              2.7                 9b682a8110bf        38 minutes ago      673 MB
    
  5. tianon commented on Jul 15, 2016

    @tianon
    Member
  6. jstewmon commented on Jul 16, 2016

    @jstewmon
    Author

    @yosifkit thanks for the great suggestion! I've implemented your proposal in #127 .

  7. brando90 commented on Oct 6, 2017

    @brando90

    so whats the solution? How do we get the library we need?

  8. brando90 commented on Oct 6, 2017

    @brando90

    whats wrong with

    apt install python3-tk
    
  9. yosifkit commented on Oct 6, 2017

    @yosifkit
    Member

    @brando90,

    whats wrong with

    apt install python3-tk
    

    We don't use the Debian/apt-provided python, but compile and install it from upstream source directly. The python3-tk package is built for the Debian python3 (which may or may not be the same version of python as the one built in the image) and also would pull in python3 from Debian and you would end up with two versions of python3 in the image.

  10. brando90 commented on Oct 6, 2017

    @brando90

    @yosifkit how do I do that?

  11. yosifkit commented on Oct 6, 2017

    @yosifkit
    Member

    @brando90, it is already installed: #127 (comment). If you need help getting it to connect to your display, it'd be better to post questions like that in the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

  12. Aburt0071 commented on Jul 8, 2018

    @Aburt0071
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions