9
$\begingroup$

This question relates to installing pip from within Blender without the need to open a terminal outside Blender. This allows for more user friendliness and pip calls from within an add-on (as opposed a terminal outside Blender: How to install Pip for Blender's bundled Python? or https://blenderartists.org/t/how-to-install-python-packages-with-pip-blender-2-8/1142721/4).

External terminal: enable pip in Blender

  1. Navigate a terminal to the folder containing python (e.g. 2.80\python\bin\python.exe)
  2. In terminal: python.exe -m ensurepip

Pip install from within Python

One way to install pip and pip packages in Blender with a terminal / IDE is:

import subprocess
import sys
# enable pip
subprocess.call([sys.executable, "-m", "ensurepip"])
# upgrade pip to latest version
subprocess.call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
# install any package
subprocess.call([sys.executable, "-m", "pip", "install", "your package"])

Blender problem

However, when executed in the Blender terminal, we get the error:

found bundled python: D:\*blender path*\blender-2.80.0-git.b46245470f79-windows64\2.80\python
unknown argument, loading as file: -m
Error: Cannot read file 'D:\*blender path*\blender-2.80.0-git.b46245470f79-windows64\-m': No such file or directory
Error: argument has no '.blend' file extension, not using as new file, exiting!
D:\*blender path*\blender-2.80.0-git.b46245470f79-windows64\-m
Saved session recovery to 'C:\Users\*name*\AppData\Local\Temp\quit.blend'

Blender quit

If we execute in the Blender terminal sys.executable, we see why: D:\\*blender-path*\\blender-2.80.0-git.ee0d8426ab6d-windows64\\blender.exe

Normally sys.executable points to a python.exe (on Windows, not tested on Linux yet).

Question

How to install pip and pip packages in Blender, from within Blender in an OS independent way?

System tested

  • OS: Windows 7 64-bit / Ubuntu 16.04 64-bit
  • Blender 2.8: ee0d8426ab6d-windows64 (2019-05-03 21:09) / 480a09a92f7f (2019-04-30 23:13)
$\endgroup$

2 Answers 2

9
$\begingroup$

Not satisfied with this, but a somehow hack-ish way:

import subprocess
import sys
from pathlib import Path  # Object-oriented filesystem paths since Python 3.4
# sys.prefix points on Windows to: 'D:\\*blender path*\\2.80\\python'
# Windows only: subprocess.call([str(Path(sys.prefix) / "bin" / "python.exe"), "-m", "ensurepip"])

# OS independent (Windows: bin\python.exe; Linux: bin/python3.7m)
py_path = Path(sys.prefix) / "bin"
py_exec = next(py_path.glob("python*"))  # first file that starts with "python" in "bin" dir
subprocess.call([str(py_exec), "-m", "ensurepip"])

You should see a blue 0 in your console in Blender.

Edit

If subprocess.call([str(py_exec), "-m", "ensurepip"]) results in a Permission Denied error (console shows 1):

  • Windows: Close Blender, right-click blender.exe and select Run as administrator. Then run the code again.
  • Linux (untested): Start Blender with the sudo command and run the code again.

Edit Blender 2.81:

Pip is now enabled by default from 2.81, so no need for this solution anymore: https://developer.blender.org/T71856

Installing a library in Blender with pip now works like this:

import subprocess
import ensurepip
ensurepip.bootstrap()
pybin = bpy.app.binary_path_python
subprocess.check_call([pybin, '-m', 'pip', 'install', 'your_package'])

Edit 2: Added pybin, '-m', otherwise it can install the package in a Python environment which is not Blender's one.

Edit 3: You first have to link pip with Blender (added ensurepip)

2023-04-27 It appears that "bpy.app.binary_path_python" has been removed.

https://projects.blender.org/blender/blender/commit/a1f46ac9dc71d66adced895a98f912a5d8440748

For blender3.2

import subprocess
import ensurepip
import sys

ensurepip.bootstrap()
pybin=sys.executable

subprocess.check_call([pybin, '-m', 'pip', 'install', 'your_package'])

For blender3.2 Check that it is actually installed.

import subprocess
import sys

blender_exec = sys.executable
subprocess.check_call([blender_exec, "-m", "pip", "list"])

enter image description here

For blender3.2 How to uninstall. (May need to reboot blender.)

import subprocess
import sys

blender_exec = sys.executable
subprocess.check_call([blender_exec, "-m", "pip", "uninstall", "-y", "your_package"])
$\endgroup$
1
1
$\begingroup$

Call this function from Blender's python installation, either within an addon (during addon registration if you prefer), from Blender's script editor, or Blender's python console.

def install_pip():
    """Bootstrap pip and any dependencies into Blender's Python configuration"""
    try:
        import pip
    except ImportError:
        print("pip python package not found. Installing.")
        try:
            import ensurepip
            ensurepip.bootstrap(upgrade=True, default_pip=True)
        except ImportError:
            print("pip cannot be configured or installed. ")
$\endgroup$

You must log in to answer this question.

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