How to write my add-on so that when installed it also installs dependencies (let's say: scipy or numpy-quaternion)?

Does blender understand setuptools (setup.py)?


Note: I'm using blender 2.8


Ran into the same issue and received a couple helpful responses on devtalk. My solution is based on the top solution in this post, but the second one ran properly as well.

import subprocess
import sys
from pathlib import Path

# OS independent (Windows: bin\python.exe; Mac/Linux: bin/python3.7m)
py_path = Path(sys.prefix) / "bin"
# first file that starts with "python" in "bin" dir
py_exec = next(py_path.glob("python*"))
# ensure pip is installed & update
subprocess.call([str(py_exec), "-m", "ensurepip"])
subprocess.call([str(py_exec), "-m", "pip", "install", "--upgrade", "pip"])
# install dependencies using pip
# dependencies such as 'numpy' could be added to the end of this command's list
subprocess.call([str(py_exec),"-m", "pip", "install", "--user", "scipy"])

Assuming the above block is incorporated into the init file of an add-on, users shouldn't need to manually install the dependencies.

- On Windows, Blender must be run in Administrator mode. In one case, I had to restart Blender before using the installed dependency.
- Installing dependencies like this requires an active internet connection

|improve this answer|||||
  • 1
    $\begingroup$ It is worth mentioning, for those pip installing dependencies that need to build, that the python sources need to be present in: PATH_TO_BLENDER/2.80/python/include/python3.7m/. Sources must match blender's python version and can be grabbed from: python.org/downloads/source. Otherwise an exception is thrown during installation mentioning the missing Python.h $\endgroup$ – gmagno Sep 24 '19 at 0:26

One possible solution is to keep add-on and dependencies installation separate, i.e:

First install deps into Blender's python:

  1. ensure Blender's python installation has pip: path/to/blender/python -m ensurepip and path/to/blender/python -m pip install -U pip
  2. And pip install your dependencies: pip install <whatever need be>
  3. If any of the deps needs to be built, as opposed to installed from wheels, you have to ensure that the python includes are present, otherwise you will get an error about missing Python.h.

And then install the add-on.

This is not an elegant solution though, having to tell your users to go through this process is not convenient.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.