My experience with Python / Matplotlib/Numpy, pip and a snap

I did not see any info on this so I wanted to share what I found.

What I wanted to do was create a snap to install a single python script. The script needed packages available as both debs and via pip3.

The package was standalone so I used :

  wrapper:
    plugin: dump
    source: files/  

Then created files/bin and placed the script in files/bin/script.py

Next I listed the packages I needed to install with the mypackages option and stage-packages within it.

mypackages:
 plugin: nil
  stage-packages:
  - python3 
  - python3-pil
  - python3-tk
  - libblas-dev

Worth pointing out, you will need libblas-dev if you want numpy to work, you will also need python3-tk if you want matplotlib to work.

Next I wanted to install a number of python packages with pip3. To do that I used python-packages.

plugin: python
python-version: python3
python-packages: 
- numpy
- WordCloud
- plotly
- matplotlib

Then I built it with snapcraft, I had to manually remove the directories: parts prime snap stage after each build or I would get the error:

Failed to run '/home/joe/newsnap/stage/usr/bin/python3 -m pip list': Exited with code 1.

I hope these notes help someone if they are building a snap with python3 packages.

The snapcraft.yaml I created is available here: https://github.com/joemcmanus/ircMiner/blob/master/snapcraft.yaml

Other notes: I tried to use the matplotlib, numpy and plotly debs but got a number of errors during runtime not during build so pip3 was the solution.

If you get:

 ImportError: 
 Importing the multiarray numpy extension module failed.  Most
 likely you are trying to import a failed build of numpy.
 If you're working with a numpy git repo, try `git clean -xdf` (removes all
 files not under version control).  Otherwise reinstall numpy.
 
 Original error was: libblas.so.3: cannot open shared object file: No such file or directory

This means you should remove the parts, primes & snaps folder and be sure to build numpy from pip3 and install the libblas-dev deb in mypackages.

If you get the following error trying to install matplotlib as via pip3:

   import tkinter as Tk
  ModuleNotFoundError: No module named 'tkinter'

Then be sure to install python3-tk as a deb in the mypackages section.

Cheers,
-Joe

2 Likes