Install pip and packages from within Blender OS independently 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&#39;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


Navigate a terminal to the folder containing python (e.g. 2.80\python\bin\python.exe)
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)
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)