Blender_Python

Intro

By default, most of the time, Blender ships with a Python repository containing a built-in version of Python. That way Blender is assured to find a coherent version of Python, the same it was compiled against. However this version is not a full install of Python and contains only the necessary libraries for Blender to run.

When you want to use external Python modules to do advanced stuff in an Add-on, you may have to use a full version of Python and set Blender to use that version. 


How to use an external version of Python ?

0) The lucky guy

On some Linux distributions, if Blender is installed from an official repository (or some contributors), it is possible that Blender has been already set up to use a system wide installation of python. This would mean that there is no need to install Python again. And you could (theoretically) jump to the fourth step. 

On Windows and OSX, it is a little different. There is rarely a system-wide installation of Python by default, or not the suitable version. 

  

1) Discovering the needed version

You need to have the same version of Python that the one used when Blender was compiled. Start Blender and open the Python Console screen. The first line should display something like: PYTHON INTERACTIVE CONSOLE 3.4.2 (default, Nov 24 2014, 15:22:10)  [GCC 4.7.1]

It means Python 3.4.2 is required, go to the Python site and get the same version for your OS.  

2) Follow the installation procedure

Unpack the archive and read how to proceed depending of your OS. On windows it should be as simple to click on Setup.exe

Basically, create a dir, rename it as "Blender_Python_Full" (or whatever) and choose it as destination for the installation.

This is not necessary to edit ENV variables to make it the default Python environment (you might even mess your system). In fact this directory of Python will just be used by Blender so no need to share it.

3) Modifying Blender accordingly

This is very simple and reversible. If you open the usual Blender archive you get from the Download server of the Blender Foundation, you will see that it contains a 2.7x directory which itself contains a directory called "python" (theses dirs may be hidden on Windows).

After unpacking, just rename that dir into "_python" and now create a soft link toward your new "Blender_Python_Full" called "python". Assure yourself that the /lib folder is directly accessible through your link like it was with the old "_python". If not Blender won't start.

Run Blender, it should start without trouble now with the full install of Python. 

Note: On OSX, Blender is packed as a dmg archive, I don't know yet how to deal with that. But it shouldn't be that different from the general case.  

4) How to install Python modules ?

Python has a nice utility to install modules called "pip". This utility is most of the time versioned and is found in the /bin directory. 

Open your CLI, go to "Blender_Python_Full/bin"  dir and type:

./pip3.4 install whatever_module

You may type :

./pip3.4 help

For all the commands ('list' for instance is useful to check the installed modules).

5) Final comments

As long as the new versions of Blender stick with the same version of Python you can re-use your full version of Python, just re-do the third step.

Some third part Add-ons may require modules that are not present by default on a shiny new install of Python. For instance Sverchock requires 'numpy', Blend4web requires 'requests' (use pip on them). Generally, if any Add-on would fail unexpectedly to initiate, start Blender from the CLI and look at the error for a missing module, then use pip.  

 

....That's it !