0

I am working under Blender 2.90 installed by snap.

If I try to run

subprocess.check_call([bpy.app.binary_path_python, "-m", "ensurepip"])

or

import ensurepip
ensurepip.bootstrap()

from within my Addon, I get told, that the site-packages of my Blender's Python are read-only. Is there any way to circumvent this?

TheBeautifulOrc
  • 555
  • 4
  • 17

2 Answers2

1

Thanks to all the help, I was able to piece together a fully automatic solution that works at least on my system (tests on other Linux and Windows machines planned). It looks something like this:

import bpy
import sys
import importlib
import subprocess

Blender's Python executable

pybin = sys.executable

def add_user_site(): # Locate users site-packages (writable) user_site = subprocess.check_output([pybin, "-m", "site", "--user-site"]) user_site = user_site.decode("utf8").rstrip("\n") # Convert to string and remove line-break # Add user packages to sys.path (if it exits) user_site_exists = user_site is not None if user_site not in sys.path and user_site_exists: sys.path.append(user_site) return user_site_exists

def enable_pip(): if importlib.util.find_spec("pip") is None: subprocess.check_call([pybin, "-m", "ensurepip", "--user"]) subprocess.check_call([pybin, "-m", "pip", "install", "--upgrade", "pip", "--user"])

def install_module(module : str): if importlib.util.find_spec(module) is None: subprocess.check_call([pybin, "-m", "pip", "install", module, "--user"])

user_site_added = add_user_site() enable_pip()

All the modules you need, that don't come shipped with Blender

modules = ["module1", "module2", ...] for module in modules: install_module(module)

If there was no user-site before...

if not user_site_added: add_user_site()

Martynas Žiemys
  • 24,274
  • 2
  • 34
  • 77
TheBeautifulOrc
  • 555
  • 4
  • 17
  • 2
    Please note that automatic loading of user site-packages was intentionally disabled, see T76993. Manually appending to sys.path works, but there may be a different preferred approach in the future if progress is made on T71420. Until then this approach is a viable alternative. – Robert Gützkow Sep 15 '20 at 18:36
  • I'll be more than happy to drop this Frankenstein of a workaround as soon as Blender offers a more elegant solution :D – TheBeautifulOrc Sep 15 '20 at 22:46
1

The paths available in Blender 3.x include /home/my-user/.config/blender/3.3/scripts/addons/modules which is writable. It is possible to install your Python packages in this directory directly. For example:

pip install --target=$HOME/.config/blender/3.3/scripts/addons/modules/ my_package

Or on Windows:

pip install --target=%appdata%\Blender Foundation\Blender\3.5\scripts\addons\modules my_package
Marc Belmont
  • 111
  • 2
  • 1
    That's %appdata%\Blender Foundation\Blender\3.5\scripts\addons\modules in Windows. One can also simply copy the module directories to the directory, from wherever they get install with system Python like maybe %appdata%\Python\Python310\site-packages – Martynas Žiemys Jun 14 '23 at 22:04