How to install python libs in Blender (part 2)

EDIT 2020: This series is now obsolete, since Blender 2.80. See part one.

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.

Introduction

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.

I encountered this problem while trying to install triangle, a “two-dimensional quality mesh generator and delaunay triangulator library”, for my tesselation addon.

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 blender/build_files/build_environment/install_deps.sh. --prefix and --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.

We then 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 modules.

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 🙂


  1. This may or may not be useful. It was sometimes needed, sometimes not; perhaps I messed up and ended up believing in the Cargo. 
Show CommentsClose Comments

6 Comments

  • Hapit79
    Posted August 1, 2018 at 10:42 pm 0Likes

    I’ve figured out a different way. Install Pip in the Blender packaged python and then use Pip to install your wanted library.

    wget https://bootstrap.pypa.io/get-pip.py
    {blenders python executable} get-pip.py
    {blenders python executable} -m pip install boto3

    Three lines of code is pretty neat. Using the blender python version fixes the diffence of the plattform python version. I only tested it on Linux, but it should work on the other plattforms as well.

    Regards
    Philipp

    • Damien Picard
      Posted August 1, 2018 at 10:53 pm 0Likes

      Thanks Philipp, I’d never heard of that, it’s really useful! I’ll test it next time I need to install new libs, and maybe update the post. 🙂

      • Damien Picard
        Posted August 1, 2018 at 11:19 pm 0Likes

        So I couldn’t wait and tested it just now. It works impeccably for simple cases, not so well for the Python module I wrote this article for, which is triangle and which uses compiled C code, and maybe some additional tricks.
        I’ll definitely update the first part of the article though!

  • Trackback: How to install python libs in Blender (part 1) - La Cuisine
  • Christian
    Posted September 30, 2019 at 9:54 am 0Likes

    I am finally happy with a combination of your solutions:

    1. Install pip the simple way from the first post.
    2. Add the header files from the python package, which the script above downloads (with correct version number) to the /include inside blenders python-path.

    Tested with dlib. Thank’s for the articles!!

    • Damien Picard
      Posted September 30, 2019 at 11:23 am 0Likes

      Christian, I’m glad you found a solution which works for you.
      Cheers!

Leave a comment