23/01/2020

Installing external python modules for Blender

Blender comes with its own Python, and it's not necessarily your system's, so just symlinking system packages like matplotlib is risky and cumbersome.
Some Googling Stackoverflow provided me with the following approach:
  • create a (temporary) virtual environment (VE) linking against Blender's python executable
  • within that VE, pip install the packages you need, but into a folder in Blender's Python path (e.g. the one in ~/.config)
  • I'd keep the VE, but Blender doesn't need it to be active to run.

In code (I keep Blender in ~/.local/bin, adapt to your setup), for matplotlib (You might have to run blender once at first to create the file structure in .config):
virtualenv --python=~/.local/bin/blender-2.81a/2.81/python/bin/python3.7m ve-blender
source ve-blender/bin/activate
mkdir -p ~/.config/blender/2.81/scripts/modules/
pip install --upgrade -t ~/.config/blender/2.81/scripts/modules/ matplotlib
deactivate


Run Blender.

Update: When I tried to do this with pickle5,  gcc failed via "Python.h: No such file or directory".

Fix: within the active VE, determine the Blender Python version (python -V).
Download the respective sources (e.g. 3.7.7) from www.python.org. and unpack.
Copy the contents of the Include directory to ~/.local/bin/blender-2.81/2.81/python/include/python3.7m/ (adapt versions and paths).

Next update:

Broken pip after initializing the virtual environment. Fixable via pip's bootstrap installer:
curl -sS https://bootstrap.pypa.io/get-pip.py | python3

Sources:
https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv
https://blender.stackexchange.com/questions/132278/how-to-make-use-of-custom-external-python-modules-in-blender-or-an-add-on-on-lin

https://blender.stackexchange.com/questions/81740/python-h-missing-in-blender-python 

https://stackoverflow.com/questions/49478573/pip3-install-not-working-no-module-named-pip-vendor-pkg-resources