08/25/2025
How-to | SDK | Python

Build Python apps: custom libs general guidelines

Why Python is difficult to cross compile?

Including python in a snap could be complicated. Python is an interpreted language but some libraries are based on ctypes. ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python. Read more here. That means that many libraries are specially designed with different packages for AMD64 and AARCH64 and this breaks the way we build our python examples if the libraries we are including have ctypes inside.

Which are the libraries with c-types?

It is not easy to make a full list of the libraries that have this problem. But for sure:

  • Tensorflow

  • Numpy

  • Pandas

  • Matplotlib

And any library that is using the BLAS and LAPACK stack has this problem. More info here.

Which is the cleanest solution?

The canonical official solution is to build over an aarch64 machine, like a raspberry PI or similar device as well as emulate an arm64 machine inside an amd64 machine with Qemu.

Snapcraft.yaml tweaks

The C/C++ libraries might not be automatically linked inside the environment. Here is the Environment variable we need to add in order to link the libraries needed for Numpy, for other libraries the present info could be incomplete. What happens here? We ensure that the app has the right folders included and the C/C++ libraries can be found. Another environmental variable that can be enhanced in some cases is the "path" library, this is helpful to include the python libraries themself.

apps:
  myapp:
    command: bin/myapp.wrapper
    daemon: simple
    plugs:
      - network-bind
      - network-status
      - network-observe
      - serial-port
      - datalayer
    environment:
       "LD_LIBRARY_PATH": "$LD_LIBRARY_PATH:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/lapack:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/blas:$SNAP/usr/lib/python3/dist-packages"
      

Include ctrlX libraries and debian packages, alternative way:

  datalayerdeb:
      plugin: dump
      source: https://github.com/boschrexroth/ctrlx-automation-sdk/releases/download/VERSION/ctrlx-datalayer-1.9.1.deb
      source-type: deb
      stage-packages:
        - libzmq5

  datalayerlibs:
    plugin: python
    build-environment:
      - PYTHONPATH: "$SNAPCRAFT_PART_INSTALL/usr/lib/python3/dist-packages"
    python-packages:
      - ctrlx_fbs
      - ctrlx_datalayer
    build-packages:
      - python3-dev
      - python3-wheel
      - build-essential
      - python3-setuptools
 
Types
How-to
Products
Controls

Latest published/updated articles