ModuleNotFoundError: No module named 'tkinter'

Hi Everyone,

I am a novice developer and I am currently developing a python tkinter snap which which displays number on the tkinter window using core18 as base. I referred to the below topic on the forum and the window appeared as expected for core 18 as base on Ubuntu 18.04:

Tkinter doesn’t work with the python plugin

When I try to change the base to core20 , the snap builds and installs without any errors on my Ubuntu 20.04 machine. When I try to run the application, I get the following error:

ModuleNotFoundError: No module named 'tkinter'

The snapcraft.yaml file is as follows:

name: tkinter-demoapp
version: '1.0'
summary: DemoApp
description: |
  Demo tkinter app example.
confinement: devmode
base: core20

grade: devel # must be 'stable' to release into candidate/stable channels


parts:

  card-uid:
    plugin: python
    source: demo-app/
    stage-packages: [python3-tk]

  debian-multiarch-triplet-provider-launch:
    plugin: nil
    stage-snaps:
      - debian-multiarch-triplet-provider-launch

  tcltk-launch:
      plugin: nil
      stage-snaps:
        - tcltk-launch

apps:
  card-uid:
    command: bin/popup
    command-chain:
      - bin/debian-multiarch-triplet-provider-launch
      - bin/tcltk-launch
plugs:
  - desktop
  - home
  - unity7
  - wayland
  - x11

# Needed by Mir and/or Xwayland
layout:
  /usr/share/X11:
    bind: $SNAP/usr/share/X11
  /usr/bin/xkbcomp:
    symlink: $SNAP/usr/bin/xkbcomp
  /usr/share/icons:
    bind: $SNAP/usr/share/icons
  /usr/share/fonts:
    bind: $SNAP/usr/share/fonts
  /etc/fonts:
    bind: $SNAP/etc/fonts

The snap is available at : https://github.com/therealdanish/Tkinter-Snap.git

Any workarounds or suggestions?

1 Like

Not sure what the issue is but I don’t think you need to upgrade to Ubuntu 20, I run 18 snaps on 20.04.

I have demo Snapcraft Tkinter code if that helps: GitHub - argosopentech/snapcraft-tkinter

Thanks @argosopentech for the repo!

I am actually trying to display the tkinter windows snap on a mir-X11-kiosk server so that it can be used in ubuntu core 18.

I have changed my snapcraft.yaml file according to the mir-x11-kiosk server template as follows:

name: x11-tkinter-demo     # YOUR SNAP NAME GOES HERE
version: '0.1'                  # YOUR SNAP VERSION GOES HERE
summary: example X11 kiosk      # YOUR SUMMARY GOES HERE
description: example X11 kiosk  # YOUR DESCRIPTION GOES HERE
base: core18
confinement: strict
grade: devel

apps:
  x11-tkinter-demo:
#    daemon: simple
#    restart-condition: always
	command-chain:
	  - env-setup
	  - bin/debian-multiarch-triplet-provider-launch
	  - bin/tcltk-launch
	command: usr/local/bin/x11_kiosk_launch $SNAP/bin/popup ### YOUR COMMAND GOES HERE

architectures:
  - build-on: amd64
  - build-on: arm64
  - build-on: armhf

parts:
  ### YOUR PART GOES HERE
  popup:
	plugin: python
   # python-version: python3
	source: demo-app/
	build-packages: [python3-tk] 
	stage-packages: [python3-tk]
	stage-snaps: [mir-kiosk-x11]

  debian-multiarch-triplet-provider-launch:
	plugin: nil
	stage-snaps:
	- debian-multiarch-triplet-provider-launch

  tcltk-launch:
	  plugin: nil
	  stage-snaps:
		- tcltk-launch    

# Needed by Mir and/or Xwayland
layout:
  /usr/share/X11:
	bind: $SNAP/usr/share/X11
  /usr/bin/xkbcomp:
	symlink: $SNAP/usr/bin/xkbcomp
  /usr/share/icons:
	bind: $SNAP/usr/share/icons
  /usr/share/fonts:
	bind: $SNAP/usr/share/fonts
  /etc/fonts:
	bind: $SNAP/etc/fonts

plugs:
  opengl:         # For Mir
  wayland:        # For Mir
  network-bind:   # For Mir (to serve X11)

When I set the base as core18, I get the following warnings while building:

This part is missing libraries that cannot be satisfied with any available stage-packages known to snapcraft:

-libffi.so.7
-libnettle.so.7
-libtinfo.so.6
These dependencies can be satisfied via additional parts or content sharing. Consider validating configured filesets if this dependency was built.

I followed this steps to set up the environment and when I run my snap, I get the following issue:

/snap/x11-tkinter-demo/x1/usr/local/bin/mir_kiosk_x11: error while loading shared libraries: libffi.so.7: cannot open shared object file: No such file or directory
/tmp/tmp.t6AEbCqa6r CLOSE_WRITE,CLOSE 
Traceback (most recent call last):
  File "/snap/x11-tkinter-demo/x1/bin/popup", line 3, in <module>
	import tkinter as tk
  File "/snap/x11-tkinter-demo/x1/usr/lib/python3.6/tkinter/__init__.py", line 36, in <module>
	import _tkinter # If this fails your Python may not be configured for Tk
ImportError: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by /snap/x11-tkinter-demo/x1/usr/lib/x86_64-linux-gnu/libpng16.so.16)
/snap/x11-tkinter-demo/x1/usr/local/bin/x11_kiosk_launch: 20: kill: No such process

When I set the base as core 20 , I do not get any errors while building but still get the ModuleNotFoundError: No module named 'tkinter' error when I run my snap.

The source code is available here

Any workarounds or modifications suggested for this issue?

1 Like

I ran into this same issue recently with a snap that has a matplotlib dependency, which depends on tkinter.

The problem seems to be that different Python paths get mixed-up, and this causes the application to be unable to find some Python modules at run time.

I was able to get around this by adding this to my PYTHONPATH for the app that is having the issue:

app-name:
   environment:
     PYTHONPATH: "$PYTHONPATH:$SNAP/usr/lib/python3/dist-packages:$SNAP/usr/lib/python3.8:$SNAP/usr/lib/python3.8/lib-dynload"

In my case, I also needed to add these layouts for the app to find matplotlib:

layout:
  /usr/share/matplotlib/mpl-data:
    bind: $SNAP/usr/share/matplotlib/mpl-data
  /etc/matplotlibrc:
    bind-file: $SNAP/etc/matplotlibrc

Hope this is useful to someone.

2 Likes

I got tkinter working on core22, this is what it might look like

name: my-snap-name
version: 0.1.0
summary: Python Tkinter test snap
base: core22
type: app
description: |
  This is a test python snap using tkinter.

grade: stable
confinement: strict

environment:
  PYTHON_SITE_PACKAGES_PATH: $SNAP/lib/python3.10/site-packages
  PYTHON_APT_PACKAGES_PATH: $SNAP/usr/lib/python3.10
  PYTHON_LIB_DYNLOAD_PATH: $SNAP/usr/lib/python3.10/lib-dynload
  TCL_LIBRARY: $SNAP/usr/share/tcltk/tcl8.6
  TK_LIBRARY: $SNAP/usr/share/tcltk/tk8.6
  PYTHONPATH: $PYTHON_SITE_PACKAGES_PATH:$PYTHON_APT_PACKAGES_PATH:$PYTHON_LIB_DYNLOAD_PATH:$TCL_LIBRARY:$TK_LIBRARY

apps:
  my-snap-name:
    command: bin/python3 $SNAP/bin/main.py
    plugs:
      - network
      - network-bind

parts:
  my-snap-name:
    source: src
    source-type: local
    plugin: dump
    stage-packages:
      - python3-tk
      - tcl-dev
      - tk-dev
1 Like