For add-ons development, sometimes it is necessary to use packages that are not included in the core set of the Blender Python interpreter. Most often, the dependencies you need are included in the Python Package Index (PyPI) and can be installed to Blender through the Pip package management system.
For Windows 10 see the updated version of this article.
The Pip Package Manager is already installed in Blender. However, Blender does not allow us to install the desired packages in a common way with the “pip install package_name” command.
To install the necessary package to Blender using Pip, we need to perform the following:
Start Blender, open the “Text Editor” window and press the “New” button to create a new script.
First, we need to get the full path to the python.exe executable for the current Blender Python interpreter.
1 2 3 4 5 6 |
import sys import os python_exe = os.path.join(sys.prefix, 'bin', 'python.exe') # 'C:\\Program Files\\blender283\\2.83\\python\\bin\\python.exe' |
Let’s perform all the next actions in a subprocess not to slow down the Blender itself.
At first, it is always recommended to check the installation of the pip module and update it to the latest version.
1 2 3 4 |
import subprocess subprocess.call([python_exe, "-m", "ensurepip"]) subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"]) |
After pip is updated we can install the necessary dependencies.
The following command will install the desired package to the Blender Python. Replace the “package_name” with the name of the desired package.
1 |
subprocess.call([python_exe, "-m", "pip", "install", "package_name"]) |
The final code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import subprocess import sys import os # path to python.exe python_exe = os.path.join(sys.prefix, 'bin', 'python.exe') # upgrade pip subprocess.call([python_exe, "-m", "ensurepip"]) subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"]) # install required packages subprocess.call([python_exe, "-m", "pip", "install", "package_name"]) |
After executing the script by clicking the “Run Script” button, the specified package will be installed to the Blender Python interpreter.
Note that for correct installation, active Internet access is required. You may also need to run Blender with administrator privileges.
Hello all !
Just a quick tip if you run into this kind of issue :
Your Blender python script won’t install the lib because it finds it inside your ‘python user’ (meaning your default OS python) site-packages, so when you import it inside your Blender script, you have an error.
1) Try uninstalling your ‘python user’ lib corresponding to the one you want to install inside blender.
2) Run your Blender script to install desired module for Blender’s python
3) Go back to your ‘python user’ and re-install the uninstalled libs from step 1.
Thank you for the tip!
Or you can copy the package that’s already installed from the OS python instead (in my case, “C:\Users\[username]\AppData\Roaming\Python\Python39\site-packages”) into Blender’s site-packages (in my case, “C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\lib\site-packages”). I did this for opencv-python and it works fine.
An interesting variant!
Thank you! This also works for older versions, such as Blender 2.79 (latest).