Snap run isn't using the right python?

So i am having a weird run issue.

snap run flexx-interface

doesn’t work - the error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/", line 86, in _run_code
    exec(code, run_globals)
  File "/snap/my-interface/x74/src/", line 20, in <module>
    from interfaces import Interface
  File "/snap/my-interface/x74/src/", line 1, in <module>
    import telnetlib3
ModuleNotFoundError: No module named 'telnetlib3'

Seems obvious. However:

  • this python lib is installed (used snapcraft debug to confirm it’s installed on host and in snap ),
  • there is only python3 and python3.10 installed on the machine and snap - no other versions. I can use both to import telnetlib3 successfully.
  • When I run my-interface (the command launch file) directly the snap runs. So in the command line if I run ./my-interface it will have no issues.

Part of my snapcraft.yaml:

    command: my-interface 
       PYTHONPATH: $SNAP/usr/lib/python3/site-packages:$SNAP/usr/lib/python3/dist-packages:

The my-interfaces file:

#!/usr/bin/env python3
import subprocess"./", cwd="src")

Any ideas why snap run is not working? I thought it may be using some wrong, hidden python version, but from what I can tell, all python versions have telnetlib3 installed.

Try setting these three env variables in your snapcraft definition:

  PYTHON_SITE_PACKAGES_PATH: $SNAP/lib/python3.10/site-packages

if push comes to shove add the below stage packages and remove what you don’t need:

  - libpython3-stdlib
  - libpython3.10-stdlib
  - libpython3.10-minimal
  - python3-pip
  - python3-setuptools
  - python3-wheel
  - python3-venv
  - python3-minimal
  - python3-distutils
  - python3-pkg-resources
  - python3.10-minimal also shows how you can include pip packages required for your snap if you do not have a and requirements.txt in your python module.

If I add the three env vars above, I get

Python path configuration:
  PYTHONHOME = '/snap/my-interface/x80/usr'
  PYTHONPATH = ':/snap/my-interface/x80/lib/python3.10/site-packages'
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = '/snap/my-interface/x80/usr'
  sys.base_exec_prefix = '/snap/my-interface/x80/usr'
  sys.platlibdir = 'lib'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/snap/my-interface/x80/usr'
  sys.exec_prefix = '/snap/my-interface/x80/usr'
  sys.path = [
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f2bd4303000 (most recent call first):
  <no Python frame>

Ok, so narrowed down the problem.

When I run --debug flag, the packages seem to be there. However when I look at /snap/my-interface/current/usr/lib/python3.10 they are not. I have tried installing these packages, both in stage-packages as well as in override-stage and in prime. They are in /snap-my-interface/usr/lib/python3/dist-packages.

In debug mode, I see it’s installed to python3 dist-packages, but not python3.10, which is what it’s using to run. Running with python3 also returns the package not found error.

Share your complete yaml please.