46
$\begingroup$

Blender comes with Python, and includes a stand-alone Python executable. However pip is not part of the official Python distribution so it needs to be added separately.

  • How do you setup pip with Blender's bundled Python so it can be used to install Python packages from Pypi which are then available from inside Blender?
  • How would you use it to install a package?

Note, this is related to this question, but not a duplicate since its not about using pip as a library.

$\endgroup$
1

11 Answers 11

53
$\begingroup$

First of all, pip is not part of Python so it doesn't come by default with Blender.

It has to be installed for Blender's bundled Python even if you already have pip for some other version of Python on your system.
For this get the get-pip.py file from the pip documentation

You'll find the blender python binary at:

/blender-path/2.xx/python/bin/python

Use this binary to run the get-pip.py. If you have it saved in your home directory the command to use should look something like this:

$ /path/to/blender/blender-path/2.xx/python/bin/python3 ~/get-pip.py

You should now have pip installed for blender. You use it with blenders python too and you have to point to the pip that was installed for blenders python. Both are in blenders folder tree and you use them like this:

$ /blender-path/2.xx/python/bin/python /blender-version/2.xx/python/local/lib/python3.5/dist-packages/pip install scipy

This installs scipy for your blenders python. Of course you have to adjust names according to the version you use, but it worked for me for 2.77.


I just tried to do this again and with a recent build I did not have to point to he installed pip, calling blenders python was enough, my command looked like:

/path/to/blenderspython/python pip install module

blender-2.8: pip is already included so the only step to do this for blender-2.8 is:

$ /path/to/blenderspython/pip install module
$\endgroup$
3
  • $\begingroup$ This worked perfectly for me. I would rather have gotten my Anaconda managed version of Python in place but I just couldn't get it to work on my Mac. $\endgroup$
    – Chuck
    Commented Oct 26, 2017 at 17:36
  • 11
    $\begingroup$ On blender-2.8 you need to run ensurepip beforehand: path/to/blender-2.8/2.80/python/bin/python3.7m -m ensurepip and then I would recommend upgrading pip: path/to/blender-2.8/2.80/python/bin/python3.7m -m pip install -U pip $\endgroup$
    – gmagno
    Commented Aug 21, 2019 at 15:05
  • 1
    $\begingroup$ This did not work for me on my Windows 7 box. After successfully installing pip as above, I get 'Defaulting to user installation because normal site-packages is not writeable (sic)'...I guess I need to add my appdata folder to my blender python path now? $\endgroup$
    – Matt
    Commented Jul 6, 2020 at 21:13
11
$\begingroup$

Pip can be installed into blender's bundled python using a package that is already in blender's bundled python, called 'ensurepip'. I did this on linux, using a direct download of blender from the site, rather than a linux distro packaged version. Open a terminal in this location /path-to-blender-download/blender-2.xxetc/2.80/python. Then run this command from the terminal, (not the blender console, not from a python session):

 bin/python3.6m lib/python3.6/ensurepip 

this tells the blender python to run the ensurepip package. This installed pip into blender-2.80etc/2.80/python/bin. So then using that version of pip:

bin/pip3 install --target lib/python3.6   packageName

So that then installs packageName into blender's bundled python, using a version of pip that is now in blender's bundled python.

The target option can be used with pip from another source on your system, to tell it to install the package into blender's python instead of the system python. For that to work, the system pip needs to be a compatible version to the blender python. The above method ensures they are compatible.

$\endgroup$
1
  • $\begingroup$ This saved my life. The Upgrade comment from the other question failed, so I tried this and it worked. $\endgroup$ Commented Mar 14, 2022 at 10:34
10
$\begingroup$

Since PIP is included with Blender from ~2.8 onward you can install things via pip directly from the Blender scripting workspace eg

#For Blender 2.92:
#You have to have admininistor priviledges so for Windows users
#JUST THIS ONCE open Blender by R-clicking on it in the start menu and select"Run as Administrator"

#Open up the System Console (under the Window menu)
#Then paste and run the following into the scripting workspace and run it
# After it has run, then close and reopen Blender normally
# You can then then use the module as normal with 
# import MODULE

#--- FROM HERE ---#

import subprocess
import sys
import os
 
# path to python.exe
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_TO_INSTALL"])

print("DONE")

However, if you use Windows 10, the python interpreter installs new packages not into the Blender installation directory, but into the personal user directory. We can solve the problem by hard-coding the directory where pip should install the required packages – the site-packages directory located inside the directory where Blender is installed.

target = os.path.join(sys.prefix, 'lib', 'site-packages')

Windows 10 script:

import subprocess
import sys
import os
 
python_exe = os.path.join(sys.prefix, 'bin', 'python.exe')
target = os.path.join(sys.prefix, 'lib', 'site-packages')
 
subprocess.call([python_exe, '-m', 'ensurepip'])
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pip'])

#example package to install (SciPy):
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'scipy', '-t', target])
 
print('DONE')

More info available here

$\endgroup$
5
  • 1
    $\begingroup$ Alas 2.93 gave this error ... Looking in links: d:\Temp\tmpipdns5nf Requirement already satisfied: setuptools in c:\program files\blender foundation\blender 2.93\2.93\python\lib\site-packages (49.2.1) Requirement already satisfied: pip in c:\users\ME\appdata\roaming\python\python39\site-packages (21.1.2) Requirement already satisfied: pip in c:\users\ME\appdata\roaming\python\python39\site-packages (21.1.2) ERROR: Could not find a version that satisfies the requirement PACKAGE_TO_INSTALL (from versions: none) ERROR: No matching distribution found for PACKAGE_TO_INSTALL DONE $\endgroup$
    – Henrik
    Commented Jun 10, 2021 at 18:10
  • 1
    $\begingroup$ Blender 3.0.0 alpha gives me the same error as @Henrik. And after running these commands Blender crashes on startup with folliwng error. Internal error initializing Python! $\endgroup$ Commented Jun 26, 2021 at 6:00
  • 1
    $\begingroup$ This occurs even when you run pip upgrade from the command prompt (eg using the Blender generated recommendation in the error message. So it's a deeper issue than my script. I'd suggest you report it as a Blender bug if it's causing you issues. $\endgroup$
    – David
    Commented Jun 27, 2021 at 9:37
  • $\begingroup$ hi guys, it worked for me on windows 7. But if you are on windows 10, there's a slight change in this script, see b3d.interplanety.org/en/… $\endgroup$
    – Harry McKenzie
    Commented Jul 5, 2022 at 5:00
  • $\begingroup$ @HarryMcKenzie Please do not use site-packages (unless this is just for your own system and you know what you are doing). This directory is intentionally excluded from the sys.path. See github.com/robertguetzkow/blender-python-examples/tree/master/… $\endgroup$ Commented Jul 5, 2022 at 9:16
6
$\begingroup$

Using pip by blender --python-expr

# Get pip: equl to /blender-path/2.xx/python/bin/python3.7m -m ensurepip
blender -b --python-expr "__import__('ensurepip')._bootstrap()" 

# Update pip toolchain
blender -b --python-expr "__import__('pip._internal')._internal.main(['install', '-U', 'pip', 'setuptools', 'wheel'])"

# pip install numpy (or any package)
blender -b --python-expr "__import__('pip._internal')._internal.main(['install', 'numpy'])"
```
$\endgroup$
2
  • $\begingroup$ Thank you for this answer! This is by far the best one for people with an automation usecase like me. All the other solutions either require you to hardcode execution paths or to run multiple python files in succession. $\endgroup$ Commented Jul 31, 2022 at 23:08
  • $\begingroup$ worked for me on Windows 11 $\endgroup$
    – Art
    Commented Dec 10, 2022 at 21:34
6
$\begingroup$

For Blender 2.93.5, this is what worked for me:

Note: I'm on Linux (Manjaro), adjust accordingly. Also, I download and use Blender directly from its folder, along with its shipped version of Python. More on that below**.

Installing pip and packages:

Ensure pip is installed, using ensurepip - more on ensurepip

./path/to/blender-2.93.5-linux-x64/2.93/python/bin/python3.9 -m ensurepip

Note: ensurepip has an --upgrade flag, but I like upgrading the traditional way; see next step. What about that -m flag?

Upgrade pip:

./path/to/blender-2.93.5-linux-x64/2.93/python/bin/python3.9 -m pip install -U pip

Install your package using pip via Python:

./path/to/blender-2.93.5-linux-x64/2.93/python/bin/python3.9 -m pip install packagename

Or install your package using pip directly:

./path/to/blender-2.93.5-linux-x64/2.93/python/bin/pip3 install packagename

Note: change packagename to an actual package name you'd like to install.

*Also note: running pip, pip3 or pip3.9 is all the same, in this case; under this context; but not if running directly from python (only use pip, in that case.) If you open these three files up in an editor you'll see they are "shortcuts" for calling the pip module.

** I like keeping separate version copies of Blender and directly using the version of Python that ships with them. I don't use the Blender from my package manager, in other words. This keeps things self contained and I don't have to mess with pyenv/virtualenv for the separate blender version of python or pollute my main system setups etc. Partly why I needed to know how to use pip directly from Blender in the first place.

Aside:

If you use VS Code to edit your python scripts for Blender and have Pylance installed, you can add this to your settings.json or *.code-workspace file:

"python.analysis.extraPaths": [
    "./path/to/blender-2.93.5-linux-x64/2.93/scripts/modules",
    "./path/to/blender-2.93.5-linux-x64/2.93/python/lib/python3.9/site-packages"
], 

Used to specify extra search paths for import resolution. source

This will turn off the warnings about not finding imports for bpy and pip installed packages, if any (adjust accordingly.)

Also, one might consider fake-bpy-module for code completions.

$\endgroup$
4
$\begingroup$

On Debian, using the Debian-packaged version of Blender, as far as I can tell, a system python executable is used.

For 2.78, I was able to do the following:

# apt-get install python3-pip
# pip3 install python-package-name

After that, the pip-installed package was available from within Blender.

$\endgroup$
1
  • $\begingroup$ If this doesn't work, delete the blender/2.78/python directory - which will make blender use the system python $\endgroup$
    – sdfgeoff
    Commented Nov 17, 2017 at 10:32
1
$\begingroup$

I'm on 2.82 on Ubuntu. I also have python 3.7, the same one Blender 2.82 requires installed on my system(and, yes, Blender still has its own Python bundled). If I run the following from inside Blender:

>>> import sys
>>> sys.path
['/snap/blender/37/2.82/scripts/startup', '/snap/blender/37/2.82/scripts/modules', '/snap/blender/37/2.82/python/lib/python37.zip', '/snap/blender/37/2.82/python/lib/python3.7', '/snap/blender/37/2.82/python/lib/python3.7/lib-dynload', '/home/yuranos/.local/lib/python3.7/site-packages', '/snap/blender/37/2.82/python/lib/python3.7/site-packages', '/snap/blender/37/2.82/scripts/freestyle/modules', '/snap/blender/37/2.82/scripts/addons/modules', '/home/yuranos/.config/blender/2.82/scripts/addons/modules', '/snap/blender/37/2.82/scripts/addons']

As you can see there's /home/yuranos/.local/lib/python3.7/site-packages, which is my system location, not Blender-specific. Hence I can install libs with my global python3.7 and have them available inside Blender right away. Seems you don't really need to bother about installing a separate pip as long as the global and the Blender version of Python match.

$\endgroup$
1
$\begingroup$

Install Packages for Windows User (After Pip works)

  1. Go to Blender Python /bin ( not just "/" !)
  2. (example) cd D:\Program Files\Blender Foundation\Blender 3.4\3.4\python\bin
  3. D:\Program Files\Blender Foundation\Blender 3.4\3.4\python\bin>
  4. Run python.exe -m pip install YOUR-Package

enter image description here

$\endgroup$
1
$\begingroup$

First make sure your Blender System Console is open by clicking menu Window > Toggle System Console so you can see installation progress.

enter image description here

Go to Scripting Tab and run the following script. It simply gets the path to your current python_exe file and installs the ensurepip module, which can install pip in a Python environment. pip is simply a thing that can install python packages such as NumPy.

import subprocess
import sys
import os

python_exe = os.path.join(sys.prefix, 'bin', 'python.exe')

subprocess.call([python_exe, '-m', 'ensurepip'])
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pip'])

This can take a while, so check your console to see the progress. Once pip has been successfully installed, you can run this script to install any python package. In this example I install the SciPy library package:

import subprocess
import sys
import os

python_exe = os.path.join(sys.prefix, 'bin', 'python.exe')
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'scipy'])

enter image description here

Replace scipy with any other library or package you need to install.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks just looking for info on this! $\endgroup$
    – Dan
    Commented Jan 9, 2023 at 6:52
1
$\begingroup$

If you are using pycharm for developing, just select the python.exe of blender as your interpreter and manage your packages through Python Packages pane Python Packages

$\endgroup$
-1
$\begingroup$

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
  • $\begingroup$ The link now shows a 404 error. Do you have any other information on the rest of that script other than what you have here? $\endgroup$
    – Tango
    Commented Mar 1, 2021 at 15:50

You must log in to answer this question.

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