ModuleNotFoundError: No module named 'tkinter'

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