45
$\begingroup$

I'm currently developing a script for Blender to handle Mesh Frequency Decomposition.

The script is nearly complete, but i need one more thing: The SciPy library to compute eigenvalues and eigenvectors.

Following the installation instructions on their site, I'm able to install it as well as Numpy. Numpy works fine, but I can't even import SciPy:

Traceback (most recent call last):
File "/CAF_FD.py", line 4, in <module>
ImportError: No module named 'scipy'
Error: Python script fail, look in the console for now...

By the way, I'm working on Linux 64 bit and I've installed SciPy and Numpy with my package manager.

Edit: When I try to use SciPy with a terminal prompt, it works.

$\endgroup$
  • 3
    $\begingroup$ did you install for python 2 or 3? Furthermore I think there are built in functions in blender to compute eigenvalues and eigenvectors. $\endgroup$ – miceterminator Dec 4 '13 at 9:57
  • $\begingroup$ Hum ... It appear that it's installed for Python 2.7. Does i have to suppose from your comment that blender uses Python 3 ? $\endgroup$ – Gouwi Dec 4 '13 at 10:21
  • 1
    $\begingroup$ This is covered by: blender.org/documentation/blender_python_api_2_69_release/… In short, you can remove Blender's local Python and the system's Python will be used instead. $\endgroup$ – ideasman42 Dec 5 '13 at 13:52
  • $\begingroup$ This answer worked fine for me - I just dragged the SciPy folder from a Python 3 Anaconda installation into Blender's Python's appropriate folder and it just worked fine (for me, then). See this comment for the details - voila! Dancing Purple Bessel Functions! $\endgroup$ – uhoh Oct 9 '16 at 15:59
44
$\begingroup$

Python3.4 in Blender is bundled within Blender. It doesn't share libraries with system's python (see this related post).

So, to use extra libraries you need to install it into /blender/2.72/python/lib/python3.4/.

OR you can remove /blender/2.72/python and Blender will fallback to using the Python installed on the system (however the versions must be compatible).

OR you can specify a folder in the script input of the User preferences:

enter image description here

Note that this folder must contain 3 sub-directories : addons, modules, and startup. So you can install the library in the modules folder.

As Gandalf3 suggested, you can find more information in the wiki here.

$\endgroup$
  • $\begingroup$ Expanding on Polosson's answer, see the wiki for more information on where to find blender's python directory. $\endgroup$ – gandalf3 Dec 4 '13 at 10:33
  • 3
    $\begingroup$ i did the following on my debian install sudo ln -s /usr/lib/python2.7/dist-packages/ply /usr/lib/blender/scripts/modules/ply to expose the ply module to blender. even though blender is compiled here with python3.2 i could use that module from 2.7 because it is compatible with 2.6 and above. $\endgroup$ – gcb Feb 1 '15 at 8:00
  • $\begingroup$ @gcb Your solution did the trick for me as well. Thanks! $\endgroup$ – mindm49907 Oct 19 '15 at 10:51
  • $\begingroup$ The tooltip in Blender 2.78 says that the required subdirs are "startup", "add-ons" (note the dash), and "modules". $\endgroup$ – Andreas Haferburg Jan 31 '17 at 14:35
  • $\begingroup$ just in case someone stumbles upon this on linux (CentOS) in turned out to be system-wide python, so I just had to do system-wide pip3.4 install X $\endgroup$ – Ben Usman Apr 20 '17 at 18:36
16
$\begingroup$

While blender's python doesn't come with pip installed, it does have ensurepip. That means that you can do something like this:

in blender's python:

>>> import sys
>>> sys.exec_prefix
'/path/to/blender/python'

then in a shell:

cd /path/to/blender/python/bin
./python -m ensurepip
./python -m pip install scipy

As Noam Peled mentions, you need to run these commands as an administrator on Windows - it probably depends on how you have blender installed on your linux machine, but you may also need to do this with escalated privileges.

$\endgroup$
  • 2
    $\begingroup$ This is the best answer for me in 2019. $\endgroup$ – off99555 Feb 18 at 10:53
  • $\begingroup$ This should be the new correct answer. Here’s a 2019 implementation $\endgroup$ – Zollie Sep 24 at 14:08
5
$\begingroup$

See this one too, it grabs the correct version for blenders python, unlike the method below:

How to use PIP with Blender's bundled Python?

By the way, I'm working on Linux 64 bit and I've installed SciPy and Numpy with my package manager.

You want to look for your installation directory of your system pythons scipy by importing the module in the console and using the __path__ method. It should print out the path where the module is located. That should point you to a folder named "site-packages" where the scipy folder should be in. Copy the whole scipy folder into the "site-packages" folder in the blender python version. Should be following a similar path as your system python.

If that doesn't work, for me it didn't, check if the version you installed is compatible with blender's python version. If it does but it still doesn't work, you can also try to download an appropriate source code package. Installation is really simple on linux. Should work with "python3 setup.py install" in the folder of the downloaded source code package where the setup.py is located.

The copying of the site package contents works for me on windows as well. I used the winpython package for that.

$\endgroup$
3
$\begingroup$

Blender now ships with a Python executable - but not distutils. You can use pip to install modules for Python, but you need to replace distutils first, and then install pip. On Ubuntu you can do something like this:

PY_BLEND=blender-2.76b-linux-glibc211-x86_64/2.76/python
cp -a /usr/lib/python3.4/distutils ${PY_BLEND}/lib/python3.4/

wget https://bootstrap.pypa.io/get-pip.py
PYTHON=${PY_BLEND}/bin/python3.4
${PYTHON} get-pip.py --user

${PYTHON} -m pip install --user -r requirements.txt

After that, you should have your modules available for import within Blender.

There is a proposal to include distutils as well which should make this easier.

$\endgroup$
1
$\begingroup$

Sometimes versions of python in Blender and in the System may differ. Along the suggestions from Polosson, I would try to

  1. install an alternate version of Python in the system:

    • download a version of Python with the same major and minor numbers as in Blender
    • compile and install in an alternate location (with configure --prefix and make altinstall)
  2. install needed modules in this alternate version (you may use pip, don't forget to use the alternate python executable, not the system one)

  3. copy what is in site-packages in the alternate version of Python to the same directory in Blender python directory.

You should now have your modules working in Blender.

$\endgroup$
1
$\begingroup$

Use latest Blender build - https://builder.blender.org/download/ - which includes submitted diff: https://lists.blender.org/pipermail/bf-committers/2016-February/046728.html that allows PIP to install properly.

It looks like using the Python version packaged with Blender is preferable as that allows for better export/packaging later on (although I have not tried this yet). Installing a matching Python system version is much easier on Windows (at least, it is vs. my Ubuntu system) and does work as I have a Blender project working exactly the same as it did with the Blender Python, with Windows system Python. On Ubuntu its very difficult to install specific Python versions as they are so keyed into system functiions. Making Python from source can add further complications but could allow a separate install. Then there's Python "virtualenv" but I have not tried that.

$\endgroup$
1
$\begingroup$

I wrote a script that install 3rd party Python modules uting pip. Here is the main function, where the full solution (python 3) for windows/mac/linux can be found here:

def install_blender_reqs(blender_fol='', gui=True):
    if blender_fol == '':
        blender_fol = find_blender()
    blender_parent_fol = get_parent_fol(blender_fol)

    # Get pip
    bin_template = op.join(get_parent_fol(blender_fol),  'Resources', '2.7?', 'python') if is_osx() else \
        op.join(blender_fol, '2.7?', 'python')
    blender_bin_folders = sorted(glob.glob(bin_template))
    if len(blender_bin_folders) == 0:
        print("Couldn't find Blender's bin folder! ({})".format(bin_template))
        blender_bin_fol = ''
        choose_folder = gui_input('Please choose the Blender bin folder where python file exists', gui) == 'Ok'
        if choose_folder:
            fol = choose_folder_gui(blender_parent_fol, 'Blender bin folder') if gui else input()
            if fol != '':
                blender_bin_fol = glob.glob(op.join(fol, '2.7?', 'python'))[-1]
        if blender_bin_fol == '':
            return
    else:
        # todo: let the user select the folder if more than one
        blender_bin_fol = blender_bin_folders[-1]
    python_exe = 'python.exe' if is_windows() else 'python3.5m'
    current_dir = os.getcwd()
    os.chdir(blender_bin_fol)
    pip_cmd = '{} {}'.format(op.join('bin', python_exe), op.join(GET_PIP_FOL, 'get-pip.py'))
    if not is_windows():
        run_script(pip_cmd)
        install_cmd = '{} install {}'.format(op.join('bin', 'pip'), REQS)
        run_script(install_cmd)
    else:
        install_cmd = '{} install {}'.format(op.join('Scripts', 'pip'), REQS)
        print(
            'Sorry, automatically installing external python libs in python will be implemented in the future.\n' +
            'Meanwhile, you can do the following:\n' +
            '1) Open a terminal window as administrator: ' +
            'Right click on the "Command Prompt" shortcut from the star menu and choose "Run as administrator"\n' +
            '2) Change the directory to "{}".\n'.format(blender_bin_fol) +
            '3) Run "{}"\n'.format(pip_cmd) +
            '4) Run "{}"\nGood luck!'.format(install_cmd))
    os.chdir(current_dir)

On Windows, you need admin privileges, to the one needs to do it manually.

$\endgroup$
  • $\begingroup$ Not sure why the -1, if this solution doesn't work for you, please describe the problem. $\endgroup$ – Noam Peled Apr 17 '18 at 17:46

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.