~2013

Python3.3 from source for Blender

It's very handy to have a full featured python 3.3 available in Blender. So this is how how you do that. For linux that is... but it should be sort of similar for Windows and OSX.

Make sure you have all the needed development libraries installed. I'm not going to tell you how it's just a bunch of lib*-dev packages.

First download the source and unpack it in a convenient directory. Then cd into the directory and run './configure'.

cd Downloads/
wget http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
tar -xjf Python-3.3.0.tar.bz2
cd Python-3.3.0/
./configure

This will check if you have all needed files and warn you if you haven't. If you haven't, fix it! :)

If you want to build python3.3 in your home directory or any other directory you have to tell ./configure this by doing.

./configure --prefix=$HOME/lib/python3.3

Now build it:

make

or if you have 2 cpu's:

make -J 2

When finished you can try 'make test' but it usually fails. You can just skipt it since it is a test. So to install issue: make install I've installed to the 'lib' dir in my home directory. Change to the directory where you installed it and to the python3.3/bin dir. Issue './python3.3' to check wether you build is working.

cd $HOME/lib/
cd python3.3/bin
$ ./python3.3
Python 3.3.0 (default, Apr  5 2013, 13:06:46)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Extra modules

Python has various ways of installing extra modules. The most basic one is to get the original source of the module and issue inside the unpacked source:

$HOME/lib/python3.3/bin/python3.3 setup.py build
$HOME/lib/python3.3/bin/python3.3 setup.py install

Distribute

Distribute is the recomended tool for installing extra modules easily. To install distribute do the following:

# download setup script
wget http://python-distribute.org/distribute_setup.py
# execute setup script
$HOME/lib/python3.3/bin/python3.3 distribute_setup.py
# remove the setup script and archive
rm distribute_setup.py
rm distribute-*.tar.gz

PIP

this needs 'distribute' installed!!!

Another way of installing extra modules is through 'pip'. You can add 'pip' to your python setup by issuing:

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$HOME/lib/python3.3/bin/python3.3 get-pip.py
# remove the setup script and archive
rm get-pip.py

Now you can easily install extra modules. For example to install 'webob' just do:

$HOME/lib/python3.3/bin/pip install webob

Easy!

Blender and Python

Blender is supplied with a minimal python version. As long as Blender finds this supplied version it will not use anything else. So the first thing we do is remove the supplied Blender python.

rm -rf /path/to/blender-2.66a/2.66/python

Now we need to tell Blender where to find python. There multiple ways to do this. The easiest is to tell Blender at startup:

PYTHONHOME="$HOME/lib/python3.3" blender

You can also set the PYTHONHOME by doing:

export PYTHONHOME="$HOME/lib/python3.3"
blender

This 'export' line you can add to your .bashrc for example.

Another way is to just replace the Blender supplied python version with the version you created.

Importing modules in Blender

If you did well you can just start Blender and it just just works. If you get errors while importing python modules like:

import webob
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ImportError: No module named 'webob'

It might be that the 'site_packages' is missing from the search path. You need to add this directory to sys.path.

>>> import sys
>>> print(sys.path)
>>> sys.path.append('/path/to/your/python/version/python3.3/lib/python3.3/site-packages')