70

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.

ideasman42
  • 47,387
  • 10
  • 141
  • 223
Gouwi
  • 701
  • 1
  • 6
  • 3
  • 3
    did you install for python 2 or 3? Furthermore I think there are built in functions in blender to compute eigenvalues and eigenvectors. – miceterminator Dec 04 '13 at 09:57
  • Hum ... It appear that it's installed for Python 2.7. Does i have to suppose from your comment that blender uses Python 3 ? – Gouwi Dec 04 '13 at 10:21
  • 1
    This is covered by: http://www.blender.org/documentation/blender_python_api_2_69_release/info_tips_and_tricks.html#bundled-python-extensions In short, you can remove Blender's local Python and the system's Python will be used instead. – ideasman42 Dec 05 '13 at 13:52
  • 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! – uhoh Oct 09 '16 at 15:59
  • The answer is here https://blender.stackexchange.com/questions/56011/how-to-install-pip-for-blenders-bundled-python/283142#283142 – Harry McKenzie Jan 07 '23 at 07:46

10 Answers10

59

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.

sambler
  • 55,387
  • 3
  • 59
  • 192
Polosson
  • 6,544
  • 8
  • 33
  • 61
  • Expanding on Polosson's answer, see the wiki for more information on where to find blender's python directory. – gandalf3 Dec 04 '13 at 10:33
  • 3
    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. – gcb Feb 01 '15 at 08:00
  • @gcb Your solution did the trick for me as well. Thanks! – mindm49907 Oct 19 '15 at 10:51
  • The tooltip in Blender 2.78 says that the required subdirs are "startup", "add-ons" (note the dash), and "modules". – Andreas Haferburg Jan 31 '17 at 14:35
  • 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 – Ben Usman Apr 20 '17 at 18:36
  • So is there a preferred method among these three? Linking to the system module as per @gcb 's suggestion works fine for devemopment, but is not portable. What is the preferred method when distributing an addon? Include the module in the package? Install via pip? Make it a user-handled requirement? – squarespiral Dec 30 '18 at 10:15
  • OR you can remove /blender/2.72/python The BEST solution! – pookie Oct 13 '21 at 11:02
49

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.

NAS
  • 591
  • 4
  • 5
12

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

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • So I could install my custom module I wrote for python (like let's say in rust), in blender, while initialising an addon right? – TheTrebuchet Jan 03 '24 at 08:59
  • if its available from pip install yourmodule, then yes @TheTrebuchet – Gorgious Jan 03 '24 at 09:21
  • 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 – TheTrebuchet Jan 03 '24 at 12:27
6

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.

B Newmark
  • 1,240
  • 1
  • 12
  • 12
4

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.

z0r
  • 877
  • 4
  • 14
2

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())
GalDude33
  • 121
  • 2
  • 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 ? – Gorgious Jun 21 '23 at 14:38
  • @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. – GalDude33 Jun 22 '23 at 16:03
  • Thank you for the explanation. :) cheers – Gorgious Jun 22 '23 at 17:07
2

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.

Noam Peled
  • 479
  • 4
  • 18
1

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.

1

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.

0

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
    
Ornstein89
  • 111
  • 1