How to install Python packages with "pip" (Blender 2.8)

Hi, I need to install a Python package (for Python bundled with Blender) and I like to use pip for this. It seems pip is not installed out-of-the box and I have tried to install it as follows (latest build of Blender 2.8):

Windows environment:

cd <path to blender>\2.80\python\bin
python.exe -m ensurepip

This installes pip here:

 <path to blender>\2.80\python\Scripts\pip3.exe

The problem is that when I list installed packages, pip will not list the packages already installed in site-packages:

cd <path to blender>\2.80\python\Scripts
pip3.exe list

Package       Version
------------- --------
pip           10.0.1
setuptools    39.0.1

E.g. “numpy” is already installed in site-packages, but not detected by pip. This causes problems when I try to install e.g. “opencv-python” as that package is depending on numpy. Pip will try to install numpy on top of the already exisiting numpy, causing errors…

I found a workaround by renaming the existing numpy to “_numpy”, then pip works as expected and installs numpy and opencv-python in site packages, I would like to avoid this hack.

Am I doing something wrong when installing pip as above?

1 Like

As you probably seen my answer for your sun hdri addon, I had to understand how to install python-opencv so I simply tried with latest build (blender-2.80.0-git.84c06e996a7a-windows64, so try using without typing that comand) using :

...\2.80\python\bin>python.exe -m pip install --upgrade pip setuptools wheel (but i’m not sure it’s necessary as it downgrade stuffs, so i probably made a mistake there but anyway at the end it worked but try first without doing that).

then

...\2.80\python\bin>python.exe -m pip install opencv-python --user

and it worked directly.

Thanks @warnotte for your help, but I’m still stuck… I just downloaded the same build, but only get:

python.exe: No module named pip

I guess I’m missing something obvious here…? As far as I can see, there is no pip available?

Edit: These are the packages I have in the Python bundled with blender (2.80\python\lib\site-packages):

certifi
chardet
idna
numpy
requests
urllib3

Comparing with a regular Python installation (…Python37-32\Lib\site-packages):

pip
pip-10.0.1.dist-info
pkg_resources
setuptools
setuptools-39.0.1.dist-info

I got it to work now as follows:

cd <path to blender>\2.80\python\bin
python.exe -m ensurepip
python.exe -m pip install --upgrade pip setuptools wheel
python.exe -m pip install opencv-python --user

I think your idea @warnotte using:
python.exe -m pip install opencv-python --user
did the trick, many thanks.

2 Likes

Yes sorry I forgot to tell i used that line ^^

Thank you for this solution. I’m adding the following workflow for those searching to combine pythons VIRTUALENV and BLENDER (like me). I’ve been struggling with this until i realized that the path blender searches for python is hardcoded. I’m using 2.79b here, but I think it may apply to ANY version.

in console
(STEP 1, duplicate shipped blender)
$ mv <blender-path>/2.79/python <blender-path>/2.79/original.python

(STEP 2, NOW create virtualenv with shipped blender)
$ virtualenv --system-site-packages -p <blender-path>/2.79/original.python/bin/python3.5m <venv-path>

(STEP 3, recreate hard coded path)
$ ln -s <venv-path> <blender-path>/2.79/python

(STEP 4, enjoy blender cli and virtualenv)
$ source <venv-path>/bin/activate
$ pip install <whatever-you-need>
$ <blender-path>/blender -b --python-console
(python)> import bpy
(python)> import pip
(python)> import <\any-package installed in virtualenv AND system-wide>

NOTE: This workflow allows you to use pdb debugger in blender scripts!
NOTE: Blender also does NOT appear to require the actual python binary <venv-path>/bin/python. Just remember that <venv-path>/bin/python CANNOT 'import bpy, You HAVE to start a blender process.
NOTE: I’m still experimenting with blender as SHEBANG (first line in script)

I hope this brings some additional value to your topic.

kind regards,
Dieter

By default pip try to setup dependency too, and by doing so might break your setup.
Eg if your package require numpy then lastest numpy will be setup over shipped one and likely break your setup.
Use the --no-deps option for the pip install command should prevent this.
Also try to setup your modules in “user” folder, as it doesn’t require administrative privileges to do so, and you’ll then have the module available for every blender’s installations.
pip --user

To sum it up
blenderpath/python -m pip install --user --no-deps your_module
where blenderpath/python is the path you can find using bpy.app.binary_path_python in the blender’s console.

Thanks for posting the solution

I used this method to attempt to install scipy.

`

ImportError: Something is wrong with the numpy installation. While importing we detected an older version of numpy in [‘C:\Program Files\Blender Foundation\Blender\2.80\python\lib\site-packages\numpy’]. One method of fixing this is to repeatedly uninstall numpy until none is found, then reinstall this version.

`
Seems like I need to uninstall the numpy which is included with blender, but, as someone noted above, it is not tracked by pip, being installed beforehand.

Advice? How to uninstall numpy by hand? Is this wise?

Any good reason to upgrade numpy ?
if no, remove the new one and keep blender’s one.
This is the purpose of --no-deps option as pip try to get latest version of deps and break things doing so.