14
$\begingroup$

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

$\endgroup$

2 Answers 2

23
$\begingroup$

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
$\endgroup$
3
  • 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
    Commented Sep 24, 2019 at 0:26
  • 1
    $\begingroup$ I don't know about older version, but to reach python executable in blender 2.83 you can use bpy.app.binary_path_python $\endgroup$
    – Pullup
    Commented Jun 26, 2020 at 15:24
  • 2
    $\begingroup$ In 3.3 binary_path_python is missing. Instead you can use import sys python_path = sys.executable $\endgroup$
    – TheJeran
    Commented Feb 20, 2023 at 11:17
4
$\begingroup$

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.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .