Update for the Snap-released Blender versions (e.g. 2.93)
In general, and for good reasons, it is not possible to modify snap installations.
This means that the procedure below will first try to install pip and the packages at the default location inside snap, but will silently fail due to the following error:
Defaulting to user installation because normal site-packages is not writeable
Then, it will install the dependencies in some other "default" location allowed (in my Ubuntu20.04 this was ~/.local/lib/python3.9/site-packages), and this is likely a place that Blender is not expecting. As a consequence, installation will be "successful" but Blender will still be unable to find the dependencies.
To diagnose this, open the Blender Python console and check sys.path to see which paths is Blender expecting, and pip show <PACKAGE> to see where has the package been installed. If the path is not in the expected list, it won't be found.
There are a few ideas to fix this:
- Force our way into snap through the OS and install files there (bad idea and likely to be a headache)
- Use
subprocess to install into snap using Blender's Python, as discussed here - Tell Blender where to look for our new files
From all 3, I personally think that option 3 is the simplest and most versatile one. We can see that Blender's Python options now include the following flag:
--python-use-system-env: Allow Python to use system environment variables such as PYTHONPATH and the user site-packages directory.
In my case, the solution was simply to install pip and the packages as before, and run Blender as before, but adding the --python-use-system-env flag when running Blender, to tell that we want to look for dependencies outside snap.
This worked like a charm for my system, but if it doesn't, an alternative is to add the desired path to sys.path before importing (e.g. by appending the path string or by updating $PYTHONPATH).
Hope this helps!
Original solution
For pip-installable dependencies (like PyPI or wheels), all you need to do is the following:
- Locate your Blender's Python binary path, let's call it
<BPYTHON> (in my case 2.80/python/bin/python3.7m at the Blender installation). The path can be retrieved from blender itself, by running the following inside the Blender Python terminal:
import sys sys.executable # Alternatively run the following one-liner, if the version is new enough blender -b --python-expr "import sys; print(sys.executable)"
- Run the following to enable pip operations in bpython:
<BPYTHON> -m ensurepip <BPYTHON> -m pip install --upgrade pip
- Now any time you want to install a package simply call pip from bpython:
<BPYTHON> -m pip install <YOUR_PACKAGE> # no --user needed <BPYTHON> -m pip install <PATH/TO/WHEEL>.whl # also works with wheels as expected
As usual, you just have to install a package once and uninstalling works the analogous way.
Blender's Python has its own environment, so this procedure will install the dependencies there (in my case the 2.80/python folder). AFAIK this will work irrespectively of your OS, system's Python version and Blender location so I found this to be most convenient. You can encounter some issues if installing cython-related packages like tkinter but this can also be fixed as I did here.
Let me know if this works out!
Cheers,
Andres