71
$\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$
5
  • 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$ Commented Dec 4, 2013 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
    Commented Dec 4, 2013 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
    Commented Dec 5, 2013 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
    Commented Oct 9, 2016 at 15:59
  • $\begingroup$ The answer is here blender.stackexchange.com/questions/56011/… $\endgroup$
    – Harry McKenzie
    Commented Jan 7, 2023 at 7:46

11 Answers 11

59
$\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$
7
  • $\begingroup$ Expanding on Polosson's answer, see the wiki for more information on where to find blender's python directory. $\endgroup$
    – gandalf3
    Commented Dec 4, 2013 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
    Commented Feb 1, 2015 at 8:00
  • $\begingroup$ @gcb Your solution did the trick for me as well. Thanks! $\endgroup$
    – mindm49907
    Commented Oct 19, 2015 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$ Commented Jan 31, 2017 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
    Commented Apr 20, 2017 at 18:36
49
$\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$
8
  • 4
    $\begingroup$ This is the best answer for me in 2019. $\endgroup$
    – offchan
    Commented Feb 18, 2019 at 10:53
  • 3
    $\begingroup$ This should be the new correct answer. Here’s a 2019 implementation $\endgroup$
    – Zollie
    Commented Sep 24, 2019 at 14:08
  • $\begingroup$ Blender still cannot find installed module. I am using Mac, in 2020. $\endgroup$
    – Cybernetic
    Commented May 8, 2020 at 22:01
  • $\begingroup$ '.' is not recognized as an internal or external command, operable program or batch file. $\endgroup$
    – June Wang
    Commented Aug 30, 2020 at 13:01
  • 1
    $\begingroup$ Worked in Ubuntu 20.04. $\endgroup$
    – user171780
    Commented Oct 28, 2021 at 23:01
13
$\begingroup$

You can install modules and their dependencies directly into the Blender /python/lib local environment folder with :

import subprocess
import sys
from pathlib import Path

py_exec = str(sys.executable)
# Get lib directory
lib = Path(py_exec).parent.parent / "lib"
# Ensure pip is installed
subprocess.call([py_exec, "-m", "ensurepip", "--user" ])
# Update pip (not mandatory)
subprocess.call([py_exec, "-m", "pip", "install", "--upgrade", "pip" ])
# Install packages
subprocess.call([py_exec,"-m", "pip", "install", f"--target={str(lib)}", "scipy"])

You just have to run this directly into the python script editor. You might need to start Blender in administrator mode.

How to run a script inside Blender

$\endgroup$
3
  • $\begingroup$ So I could install my custom module I wrote for python (like let's say in rust), in blender, while initialising an addon right? $\endgroup$ Commented Jan 3 at 8:59
  • $\begingroup$ if its available from pip install yourmodule, then yes @TheTrebuchet $\endgroup$
    – Gorgious
    Commented Jan 3 at 9:21
  • $\begingroup$ Thanks for the reply @Gorgious. Well I suppose I can make a wheel, put it in the addon files and the pip should just install it if I give it the path. I have kind of mixed feelings about this whole thing, like writing addons with c or rust and python for blender should be easier, writing everything in python is just way too expensive $\endgroup$ Commented Jan 3 at 12:27
6
$\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$
4
$\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$
2
$\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$
1
  • 1
    $\begingroup$ Not sure why the -1, if this solution doesn't work for you, please describe the problem. $\endgroup$
    – Noam Peled
    Commented Apr 17, 2018 at 17:46
2
$\begingroup$

Blender python console doesn't have the path to the global site package directory and because of permissions limitation or the way you installed the specific package the installation often resides there.

You can add the global site package directory to your path hardcoded like this:

import sys
sys.path.append('<path to global site package directory>')

If you don't want to hardcode the global site package directory, you can add the global user site packages to your python path using this code:

import sys
import site
sys.path.append(site.getusersitepackages())
$\endgroup$
3
  • $\begingroup$ Hey thanks for the answer, I'm curious, isn't there a risk of compatibility issue if the user installed numpy in their global package directory in addition to the one that comes shipped with Blender ? $\endgroup$
    – Gorgious
    Commented Jun 21, 2023 at 14:38
  • $\begingroup$ @Gorgious I think it's OK if you import numpy before adding the path, and because we are using append here to add the new path it should search in blender python repo before anyway. But of course this method is not perfectly safe, but can help if you can't install directly to blender's python dir. $\endgroup$
    – GalDude33
    Commented Jun 22, 2023 at 16:03
  • $\begingroup$ Thank you for the explanation. :) cheers $\endgroup$
    – Gorgious
    Commented Jun 22, 2023 at 17:07
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$
0
$\begingroup$

Rather simple method fow MS Windows, requires no special permissions, and works from script code itself (https://stackoverflow.com/a/68964354/12691808):

  1. Install the package you want right in the script with user-level permissions:

    import bpy, pip
    pip.main(['install', 'scipy', '--user'])
    
  2. Actual installation path of user-level installation is %AppData%\\Python\\<blender python version>\\site-packages, add that path to %PATH%:

    import sys
    packages_path = "C:\\Users\\<username>\\AppData\\Roaming\\Python\\Python39\\site-packages"
    sys.path.insert(0, packages_path )
    
  3. Make sure your package imported with no errors:

    import scipy
    
$\endgroup$
0
0
$\begingroup$

I tried all the other answers, and they mostly didn't work. The main issue I was facing is that I needed something that I could run without administrative privileges. In particular, I needed something I could hand off to a designer that did not require them to do more than click a button. So now, all a designer has to do is run this script before running any of the other ones and it will modify sys.path for the duration of their session while also asserting dependencies are available.

import bpy
import sys
import site
import importlib
import os
import sysconfig
import pip

# Add global and user-specific site-packages paths to sys.path
paths = [sysconfig.get_path("purelib"), site.getusersitepackages(), *site.getsitepackages()]
for path in paths:
    if path not in sys.path:
        sys.path.append(path)

# Refresh import system caches
importlib.invalidate_caches()

# Dependencies to check and install
dependencies = [("PIL", "Pillow")]

# Check and install missing dependencies
for package, install_name in dependencies:
    try:
        __import__(package)  # Try importing the package
        print(f"{package} is installed and working!")
    except ImportError:
        print(f"Installing {install_name}...")
        pip.main(['install', install_name, '--user'])
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .