Can I install Pandas (or other modules) into Blender's Python?

I understand blender runs a version of python separate from the system python. I’m writing some scripts to collect a bunch of data on my objects, investigate it, and spit out some cool graphs. I usually do this in the context of a Jupyter notebook with Numpy, Pandas, and Matplotlib. Blender already has Numpy, so I just have to figure out how to get the other two in there.

Thanks for the help!

1 Like

You can- you just have to use pip. The code looks something like this:

import sys
import subprocess
import os
try:
    from <package-name> import <function>
except:
    python_exe = os.path.join(sys.prefix, 'bin', 'python.exe')
    # upgrade pip
    subprocess.call([python_exe, "-m", "ensurepip"])
    subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"])
    # install required packages
    subprocess.call([python_exe, "-m", "pip", "install", "<package-name>"])

The try/except is because you only need to install the package once, otherwise Blender will lag a ton each time you open it. If the package is already installed, the try just imports what you need from it. If it’s not, the except catches it and installs it. This process is a bit slow on the first time (initial install), but fortunately once it’s installed there’s no more lag :slight_smile:

4 Likes

Had to modify it a little for my linux box, but this was perfect. Thank you!

[EDIT] In case someone is reading this down the road, everything is exactly the same except the executable in sys.prefix/bin/ isn’t ‘python.exe’ it’s just ‘python’ or ‘pythonX.Xx’. The python_exe variable can be whatever you want, naturally.

1 Like

Oh yeah, my bad, that’s windows specific code :sweat_smile: I’m glad you could figure it out from there!

1 Like

and for MacOs users, this worked for me:

import sys
import subprocess
import os

def python_exec():
    import os
    import bpy
    try:
        # 2.92 and older
        path = bpy.app.binary_path_python
    except AttributeError:
        # 2.93 and later
        import sys
        path = sys.executable
    return os.path.abspath(path)


try:
    from pandas import read_csv
except:
    
 
    python_exe = python_exec()
   # upgrade pip
    subprocess.call([python_exe, "-m", "ensurepip"])
    subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"])
   # install required packages
    subprocess.call([python_exe, "-m", "pip", "install", "pandas"])

i also made a small video tutorial about it: https://youtu.be/2zGIERHU2mk

2 Likes

Wow, this script is really cool, especially for installing multiple libraries!

But a quick reminder that you can also install any API from the command line of your terminal app.

Start with the path to the python folder in your Blender app bundle, then -m (which stands for module-name), and then the install command.

For example, anytime I download a new version of Blender, I open Terminal (I’m on a Mac) and run:

/Applications/Blender.app/Contents/Resources/3.4/python/bin/python3.10 -m pip install --upgrade pip
/Applications/Blender.app/Contents/Resources/3.4/python/bin/python3.10 -m pip install --upgrade Pillow

Caveat for Mac users – before you make any changes to a fresh install of Blender, make sure to launch it once so your Mac recognizes it as safe software. I’ve found if I modify anything in the app bundle before I run it, my Mac considers it as malware and won’t run it at all.

So anyway, installing Pandas and Matplotlib from the Terminal would be:

/Applications/Blender.app/Contents/Resources/3.4/python/bin/python3.10 -m pip install pandas
/Applications/Blender.app/Contents/Resources/3.4/python/bin/python3.10 -m pip install matplotlib

Run them separately, of course.

Forgive me for re-stating what is obvious to power users, but since this is in the Python Support channel, I’m throwing this out for our comrades who are still new to the basics (like me!) and come across threads like this at the start of their journey. :fist:

2 Likes

Nice! The little advantage about the script is: you can run it in any Blender version and it will install the library you want for that Blender version. If i do it by terminal, i always have to change the path to the “right” path manually :wink: And if the Blender developers change the path to Blender, the script still will work.

3 Likes

I am one of noobs mentioned above and I can’t for the life of me get a script running even after the things mentioned.

A simple run of this script

nets me
ModuleNotFoundError: No module named 'scipy'

even after trying to install 8 ways from Sunday.

Running sudo /Applications/Blender.app/Contents/MacOS/Blender
to make sure I get to see the console, and after running I’m still getting ModuleNotFoundError despite getting Requirement already satisfied for scipy.

I want to just throw my computer out right now.

This worked perfectly also on Windows, while the previous ones didn’t. Thanks

1 Like

Hi and thanks a lot for these scripts!
However it does not work for me. I’m using Blender 4.2 under windows, and python.exe of blender does not want to install in the blender site-packages. It installs in the system python site-packages instead.

python_exe variable is
C:\Program Files\Blender Foundation\Blender 4.2\4.2\python\bin\python.exe

but when trying to do any pip command, it tells me

Defaulting to user installation because normal site-packages is not writeable

and then i get messages telling me that the package is installed in the system python installation.

Tried to change the permissions of site-package folder and it does not work.
has anyone had the same issue?
Thanks for your help!

François

1 Like