Using opencv and gobject-introspection from python within a snap

Hi,

I’m trying to package a python tool that uses the opencv and gexiv2 (via gobject-introspection) bindings.

My snapcraft.yaml file looks like:

name: pictool
version: git
grade: devel
summary: command line program for manipulating and altering images
description: |
  pictool is a command line program for manipulating and altering images.
base: core20
confinement: strict

parts:
  pictool:
    plugin: python
    source: .
    python-packages:
      - pip
      - setuptools
      - wheel
    stage-packages:
      - python3-pbr
      - python3-opencv
      - opencv-data
      - gir1.2-gexiv2-0.10
apps:
  pictool:
    command: bin/pictool

When I install the snap and try to run pictool, I get:

$ pictool
debugging sys.path:  ['/snap/pictool/x12/bin', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/snap/pictool/x12/lib/python3.8/site-packages']
Traceback (most recent call last):
  File "/snap/pictool/x12/bin/pictool", line 5, in <module>
    from pictool import main
  File "/snap/pictool/x12/lib/python3.8/site-packages/pictool/__init__.py", line 28, in <module>
    import pictool.utils_opencv as utils_opencv
  File "/snap/pictool/x12/lib/python3.8/site-packages/pictool/utils_opencv.py", line 23, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

The first line show sys.path for debugging.
Looking at the content from the snap, the needed cv2 import is located at
/snap/pictool/current/usr/lib/python3/dist-packages/:

$ ls -al /snap/pictool/current/usr/lib/python3/dist-packages/
total 6808
drwxr-xr-x  7 root root     158 Dez 15 21:53 .
drwxr-xr-x  3 root root      36 Dez 15 21:53 ..
-rw-r--r--  1 root root 6970496 Feb 18  2020 cv2.cpython-38-x86_64-linux-gnu.so
drwxr-xr-x  3 root root      32 Dez 15 21:53 gi
drwxr-xr-x 15 root root     433 Dez 15 21:53 numpy
drwxr-xr-x  2 root root     124 Dez 15 21:53 numpy-1.17.4.egg-info
drwxr-xr-x  5 root root     298 Dez 15 21:53 pbr
drwxr-xr-x  2 root root     124 Dez 15 21:53 pbr-5.4.5.egg-info

but this directory is not in the python path.
Is that a bug of the python module? Should I set PYTHONPATH somehow?

Where does pictool come from? does it have the correct shebang (#!..env $SNAP/usr/bin/python3)? From the looks of your sys.path, it does not.

Looks like the shebang is wrong:

$ cat /snap/pictool/current/bin/pictool 
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import sys
from pictool import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

Solved now. See https://github.com/toabctl/pictool/blob/master/snap/snapcraft.yaml .

It now looks like this:

name: pictool
version: git
summary: command line program for manipulating and altering images
description: |
  pictool is a command line program for manipulating and altering images.
base: core20
confinement: strict

parts:
  pictool:
    plugin: python
    source: .
    build-environment:
      - SNAPCRAFT_PYTHON_VENV_ARGS: --system-site-packages
    stage-packages:
      # from https://github.com/snapcore/snapcraft/blob/master/tests/spread/plugins/v2/snaps/python-hello-staged-python/snap/snapcraft.yaml
      - libpython3-stdlib
      - libpython3.8-stdlib
      - libpython3.8-minimal
      - python3-pip
      - python3-setuptools
      - python3-wheel
      - python3-venv
      - python3-minimal
      - python3-distutils
      - python3-pkg-resources
      - python3.8-minimal
      # pictool stuff
      - python3-opencv
      - opencv-data
      - python3-gi
      - gir1.2-gexiv2-0.10
      - gir1.2-glib-2.0

apps:
  pictool:
    command: bin/pictool
    plugs:
      - network
      - home
    environment:
      LD_LIBRARY_PATH: $SNAP/lib:$SNAP/lib/$SNAPCRAFT_ARCH_TRIPLET:$SNAP/usr/lib:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/blas:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/lapack
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:$SNAP/lib/python3.8/site-packages/
      GI_TYPELIB_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/girepository-1.0