14

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)?

Thanks,

Note: I'm using blender 2.8

gmagno
  • 402
  • 4
  • 13

2 Answers2

23

Ran into the same issue and received helpful responses on devtalk. There's also another post that was helpful when installing pip for 2.81.

2.81

import subprocess
import bpy

py_exec = bpy.app.binary_path_python

ensure pip is installed & update

subprocess.call([str(py_exec), "-m", "ensurepip", "--user"]) 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"])

Modification for 3.6

import sys

py_exec = sys.executable

Warnings:

  • On Windows, Blender must be run in Administrator mode. Blender may need to restart before using the installed dependency.
  • Installing dependencies requires an active internet connection
logicray
  • 103
  • 3
Zollie
  • 428
  • 3
  • 15
  • 1
    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: https://www.python.org/downloads/source/. Otherwise an exception is thrown during installation mentioning the missing Python.h – gmagno Sep 24 '19 at 00:26
  • 1
    I don't know about older version, but to reach python executable in blender 2.83 you can use bpy.app.binary_path_python – Pullup Jun 26 '20 at 15:24
  • 2
    In 3.3 binary_path_python is missing. Instead you can use import sys python_path = sys.executable – TheJeran Feb 20 '23 at 11:17
4

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.

gmagno
  • 402
  • 4
  • 13