If you are doing Blender scripting, sometimes you might want to do something very specific that requires one or more third party modules that are not available by default. Let’s see how you can install Python modules in Blender.
There are a few methods to do it, I believe that the one I will describe, that uses pip, is the simplest one. What I am showing was tested on Blender 2.79 and Blender 2.80 beta. I am working on Windows, however the steps should be similar also on the other operating systems.
1. Locate Blender’s bundled Python
Blender ships with its own version of Python. It should be in the Blender’s folder under
/blender/2.xx/python/ . However, to be sure, in the Python console of Blender you can type:
import sys sys.exec_prefix
and you will have your answer.
As a side note, I will say that if you want you can force Blender to use another another Python that you have installed on your machine. To do so just remove the Blender folder that you have just located and Blender will fallback to use the system’s Python. However, I prefer to stick with Blender’s own version of Python, keeping things separated from other projects.
Run Power Shell as administrator and navigate to the Blender’s Python folder.
2. Install pip
We now need to install pip. If you don’t know what pip is, you can read about it here and here: it is a super-easy way to install new modules in Python.
Using Blender’s python (that should be in the bin folder) run the module ensure pip, like in the image below.
3. Install Python modules in Blender using pip
pip is now in ./python/scripts and we can now use it to install new modules in Blender. It is important to stress that you have to use THIS version of pip, not the one that might already be available on your computer as environment variable, otherwise the new packages will not be visible to Blender.
In the example below I used pip to install the scikit-image module for a new project that I am working on.
Test the installation from the command line with Blender’s bundled Python or directly from Blender’s Python console. If there are errors read them and try to act accordingly.
THANKS:
I wrote this article after making some research on Blender Stack Exchange, in particular:
https://blender.stackexchange.com/questions/56011/how-to-install-pip-for-blenders-bundled-python
https://blender.stackexchange.com/questions/5287/using-3rd-party-python-modules?
https://blender.stackexchange.com/questions/5287/using-3rd-party-python-modules?noredirect=1&lq=1
Interested in Sverchok? Then check out Learning Sverchok, the free ebook by CodePlastic!
It says “No module named ensurepip”
Thank you, you saved my life. is there any method to execute Blender in a sort of python virtual enviroment?
Thank you, again.
Hi, you might want to check using Blender from the command line: https://docs.blender.org/manual/en/latest/advanced/command_line/index.html In particular, notice how you can use it to run Blender Python scripts https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#python-options.
Alternatively, you could try to use Blender as a Python module (I haven’t tried this but here there are some clues https://blender.stackexchange.com/questions/117200/how-to-build-blender-as-a-python-module).
Cheers