2
$\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
$\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.
$\endgroup$
0
$\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$

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.