We can install additional python module packages to Blender, which are not installed by default, with pip. However, if we use Windows 10, the python interpreter installs new packages not into the Blender installation directory, but into the personal user directory.
During installation, pip will show the following warning in the system console:
Defaulting to user installation because normal site-packages is not writeable
The packages will be installed, but into the directory:
c:\Users\_windows_user_name_\AppData\Roaming\Python\Python39\site-packages\
As a result, packages are installed but are not available inside Blender – they cannot be accessed from the Python console or Blender Text Editor using the “import” command.
We can solve the problem by hard-coding the directory where pip should install the required packages – the “site-packages” directory located inside the directory where Blender is installed.
Let’s define 2 variables – in the first, we will set the path to the executable Blender python.exe file, in the second – the path where the packages would be installed.
1 2 3 4 5 6 7 8 |
import sys import os python_exe = os.path.join(sys.prefix, 'bin', 'python.exe') # C:\Program Files\blender3-0\3.0\python\bin\python.exe target = os.path.join(sys.prefix, 'lib', 'site-packages') # C:\Program Files\blender3-0\3.0\python\lib\site-packages |
First, upgrade pip:
1 2 3 4 |
import subprocess subprocess.call([python_exe, '-m', 'ensurepip']) subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pip']) |
And install the required package, specifying the destination directory:
1 |
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'scipy', '-t', target]) |
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import subprocess import sys import os python_exe = os.path.join(sys.prefix, 'bin', 'python.exe') target = os.path.join(sys.prefix, 'lib', 'site-packages') subprocess.call([python_exe, '-m', 'ensurepip']) subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pip']) subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'scipy', '-t', target]) print('FINISHED') |
Now, the packages will be installed correctly, in a directory inside Blender, and can be used in it.
1 |
import scipy |
If I install package with this code without admin privileges, it write in console:
“PermissionError: [WinError 5] Отказано в доступе: ‘C:\\Program Files\\Blender Foundation\\Blender 3.3\\3.3\\python\\lib\\site-packages\\numpy\\array_api\\linalg.py'”
Hi!
This means that you have troubles with the numpy package. Maybe numpy is no longer support win-10. Try to get more old version and reinstall it to downgrade version on the numpy package.
If you have installed a number of extra libraries via pip to a blender install, and you update blender, how do you advise ‘mirroring’ your installed packages for the latest install of blender? eg going from 3.3 up to 3.4
Simply reinstall them.
Another variant, I think, is to copy all the site-packages directory to new version of Blender.
I did think about this. My first thought was ‘what if a package I copy over has been updated and gets overwritten with the old one. not sure whether this is possible or not. I’m trying to be careful about what a best practice might be.
Yes, this could be. And some dependencies could not or be updated and cause conflicts The clearest way, I think, is to reinstall packages.