0

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

Karan
  • 1,984
  • 5
  • 21

2 Answers2

1

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__

unwave
  • 1,643
  • 5
  • 14
  • It doesn't works – Karan Aug 23 '22 at 11:03
  • @Karan What is the error? – unwave Aug 23 '22 at 13:31
  • I tried and it's not installing any python package anywhere – Karan Aug 23 '22 at 13:40
  • What does it write to the console? Can you import the modules? The destination for the installed modules is site.getusersitepackages(), see what is the return value of it and look for the modules there. – unwave Aug 23 '22 at 13:45
  • I can't use import <module> inside blender python console, and I can't use import <module> in my addon files. It says not found – Karan Aug 23 '22 at 13:49
  • Are the modules present in site.getusersitepackages()? Is site.getusersitepackages() in sys.path when you import? – unwave Aug 23 '22 at 13:51
  • Yeah, it is available – Karan Aug 23 '22 at 13:53
  • This script uses the user's site-packages folder from the main python interpreter on the system, instead of the one used by Blender. Mixing these is a bad idea and could easily lead to problems. A better idea is to install the module to the addons directory to keep everything encapsulate. This location can be found with 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
  • @Jasper A problem with just 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
  • Some cases of the Blender's python version changing are covered by the logic in the PREFERENCES_OT_copy_prev operator.

    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:49
0

I 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:

enter image description here

james_t
  • 5,446
  • 8
  • 29