I want to install the python module via pip before installing the addon.
The addon depends on the module which I want to install via pip
I want to install the python module via pip before installing the addon.
The addon depends on the module which I want to install via pip
In the __init__.py inside def register() and before the modules imports. It does not import the modules, only installs. The function should be run every time the addon is activated as it adds required path site.getusersitepackages() where the installed modules are located to sys.path. While installing Blender may appear frozen, but it actually downloads the modules. Example.
Get ensure_site_packages code from here: https://gist.github.com/unwave/24f0ee4bfc9cc0af1bf35eda5d54d49f
import site
ensure_site_packages.ensure_site_packages([
("PIL", "Pillow"),
("xxhash","xxhash"),
("bs4","beautifulsoup4"),
("pyperclip", "pyperclip"),
("cv2", "opencv-contrib-python"),
], site.getusersitepackages())
Can also use ensure_site_packages.get_site_packages_directory() which is .\_deps\<python version> relative to ensure_site_packages.__file__
import <module> inside blender python console, and I can't use import <module> in my addon files. It says site.getusersitepackages()? Is site.getusersitepackages() in sys.path when you import?
– unwave
Aug 23 '22 at 13:51
bpy.utils.user_resource("SCRIPTS", path="addons/modules", and pip takes a -t location argument to install to the given location instead.
– Jasper
Nov 07 '22 at 10:02
addons/modules is that it gets copied when you choose to transfer the previous Blender preferences to a newer Blender which can have a different Python version. So the distinction between python37/site-packages and python311/site-packages is lost. The site.getusersitepackages() was added to sys.path by default: https://projects.blender.org/blender/blender/commit/72c012ab4a3d2a7f7f59334f4912402338c82e3c
– unwave
May 01 '23 at 18:29
https://github.com/blender/blender/blob/f33d7bb598ceae93af4e8eb1cee79e06a1b8950c/scripts/startup/bl_operators/userpref.py#L72-L158C28 https://github.com/blender/blender/blob/f33d7bb598ceae93af4e8eb1cee79e06a1b8950c/scripts/startup/bl_operators/wm.py#L3193-L3201C4
But for example 2.93 would suggest to transfer the settings from 2.90 despite the fact that they use 3.9 and 3.7 python versions correspondingly.
– unwave Jul 01 '23 at 14:49I see that the pip package is already included with blender 3.2. On the python console try the command import pip and then pip.[Tab autocomplete] to verify that it is able to navigate.
If you need you can install packages within your personal scripts folder that you can find (or change) via Edit :: Preferences :: File Paths :: Scripts: