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
- 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)