11
$\begingroup$

I'd like to install some python modules which need to be compiled and use them in Blender. Usually I'd pip install them but that doesn't seem to be a possibility for Blender. I tried copying sys.path from my system python environment but it came up with quite a few errors.

The modules are cython and mdtraj - both are available using anaconda and pip but both need to be compiled for the python environment they're supposed to be run on.

So how should I go about installing/setting those up for use in Blender?

$\endgroup$
1
7
$\begingroup$

You need modules that work with the same Python version as the one shipped with blender (currently, blender 2.78 is shipped with Python 3.6.0).

My favorite Python distribution is anaconda. Here are the steps I just followed for blender 2.78 in linux (a similar setup should be easy to get for windows or macos).

  1. Install anaconda.
  2. Create an specific environment for the modules you need in blender:
conda create --name conda-python-blender python=3.6.0
  1. Activate this environment:
source activate conda-python-blender
  1. Install all your required libraries ("modules"):
conda install cython
  1. You can also use pip if your library is not (yet) in anaconda repositories:
pip install mdtraj
  1. Figure out the directory where your libraries are stored:
echo "import sys; print(sys.path)" | python

In my case, I get:

['', '/home/christophe/anaconda3/envs/conda-python-blender/lib/python36.zip', '/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6', '/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6/lib-dynload', '/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6/site-packages', '/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg']

You want the "site-packages" directory, so, in my case:

/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6/site-packages
  1. Open blender, then go to the Python console and do the following as Rich Colburn said, that is adding the previous directory the the path where Python is looking for libraries:
  import sys
  sys.path.append('/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6/site-packages')

And that's it!

  1. Optional step. If you want to make it permanent so that every time you open a new blender file, blender adds automatically the required directory to the path, then save the following lines in a file (say add-anaconda-libs-to-path.py) and put the file in ./scripts/startup/ (for me it is /home/christophe/.config/blender/2.78/scripts/startup/.
import sys
sys.path.append('/home/christophe/anaconda3/envs/conda-python-blender/lib/python3.6/site-packages')
def register():
    print("Added anaconda library to path")

A final note: unless you want to update your libraries with conda, you don't have to activate the conda-python-blender environment anymore as blender will use its own Python interpreter.

$\endgroup$
4
$\begingroup$

Other answers manipulate the PATH and trick Python into detecting packages that were installed for another Python environment, this can lead to version errors, other problems and is ultimately a roundabout solution.

The most direct solution is to use python -m pip install <packages>, where python needs to be the Python executable that Blender comes installed with.

To detect the executable Blender uses, run the following code inside of Blender:

import sys; print(sys.executable)

Let's say that prints /usr/local/blender/bin/python then the complete command for installing numpy and scipy would be:

/usr/local/blender/bin/python -m pip install numpy scipy
$\endgroup$
1
  • $\begingroup$ Agree re not appending to path. IMO tho the accepted answer, ie up to making a virtual environment is the go. (doesn't have to be conda) Then start blender using that environment. by setting the BLENDER_SYSTEM_PYTHON environment variable, or starting via blender --env-system-python pointed at the venv. This way will have a vanilla blender python install to fall back to if things go awry... or for testing. . There are a number of questions here where using the method outlined in your answer has not worked $\endgroup$ – batFINGER Apr 9 at 18:03
2
$\begingroup$

I was able to get scipy to load just now by doing this:

import sys
sys.path.append('C:\\Users\\me\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages')
import scipy

The site-packages is the folder containing the scipy folder

$\endgroup$
1
  • $\begingroup$ This works quite well provided conda python version is the same as python shipped with blender. For blender 2.78, be sure to install python 3.6.0 with conda. $\endgroup$ – cjorssen Mar 21 '17 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.