In this article about installing third-party python libraries, we go deeper when installing with pip is not enough and you need to compile the library.
In a previous article, I explained how to install such a python module as BeautifulSoup in Blender.
I shall now move on to a harder case: installing a module when the system version of Python is incompatible with Blender’s one.
When going the usual route, that is: creating and sourcing a temporary virtual environment, installing the module through
pip, and copying it to blender’s
modules directory, it failed miserably with a
undefined symbol: PyFPE_jbuf Error. From what I could gather from a web search, this was apparently due to an incompatible numpy version…
One big script
Without further ado, here’s a magical script that will solve your problems:
cd /tmp wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tgz tar -xvzf Python-3.5.3.tgz mkdir python_bin cd Python-3.5.3/ ./configure --prefix=/tmp/python_bin --libdir=/tmp/python_bin/lib \ --with-computed-gotos --with-pymalloc make -j8 && make install cd .. virtualenv -p python_bin/bin/python3 venv source venv/bin/activate pip install triangle --no-cache-dir cp -r venv/lib/python3.5/site-packages/triangle \ ~/.config/blender/2.79/scripts/modules
lol of course it won’t. Here is what it does, expanding upon what we saw last time.
cd /tmp wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tgz tar -xvzf Python-3.5.3.tgz mkdir python_bin cd Python-3.5.3/
This gets (
wget) the sources for the python version matching with the one in blender 2.79. It also extracts the archive (
tar -xvzf). Everything will happen in /tmp, so that we don’t have build files lying around for ever.
python_bin will be the destination dir for the python binary.
./configure --prefix=/tmp/python_bin --libdir=/tmp/python_bin/lib \ --with-computed-gotos --with-pymalloc make -j8 && make install
This is where the magic happens. This is just the standard procedure for building and installing python. The trick is to use the same configuration options as blender’s python (
--with-computed-gotos --with-pymalloc) 1, found in the blender sources, in
--libdir specify a destination directory. This is to avoid overwriting the system python install with this one. Alternatively, one could use
make altinstall instead of
make install, to install this python version alongside the one existing in the system. I have got to admit that I have no idea what these options actually do, I only know that they somehow make the compiled module compatible with the blender numpy version.
cd .. virtualenv -p python_bin/bin/python3 venv source venv/bin/activate pip install triangle --no-cache-dir cp -r venv/lib/python3.5/site-packages/triangle \ ~/.config/blender/2.79/scripts/modules
This is the final stroke. After going back to /tmp, we create and source a virtualenv in the same way as in part 1, with the difference that we instead use our newly compiled binary.
pip install triangle. The
--no-cache-dir is there to avoid using a cached compiled lib, in case we already tried to install it and messed up (this has happened to me a lot).
At last, we copy the module to blender’s
The final step would be to run
import triangle in Blender, to check that the dreaded error has gone. If it has, you can finally relax, or start the real work of coding your addon!
Happy scripting 🙂
- This may or may not be useful. It was sometimes needed, sometimes not; perhaps I messed up and ended up believing in the Cargo. ↩