26
$\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$
37
$\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$
  • $\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 Oct 26 '17 at 17:36
  • 1
    $\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 Aug 21 at 15:05
4
$\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$
2
$\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$
  • $\begingroup$ If this doesn't work, delete the blender/2.78/python directory - which will make blender use the system python $\endgroup$ – sdfgeoff Nov 17 '17 at 10:32
0
$\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$

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.