Skip to content

Instantly share code, notes, and snippets.

@alchem0x2A
Last active May 29, 2024 13:14
Show Gist options
  • Save alchem0x2A/345e6b7d8ea68087db3fa25ea7268cb0 to your computer and use it in GitHub Desktop.
Save alchem0x2A/345e6b7d8ea68087db3fa25ea7268cb0 to your computer and use it in GitHub Desktop.
Enable 3rd-party packages in Blender using virtualenv

Install external python packages + virtualenv with Blender

Requirements: Blender 2.80+ (2.79 should also work with pip method, not tested thoroughly)

Step 1: Install virtualenv on the system

In order not to mix the packages installed for Blender (such as scipy, matplotlib) with those system-wide packages, we need to create a virtual environment for the Blender alone.

Best practice is to use a single virtualenv for each version of Blender. See the following steps:

  • Find the python binary bundled with Blender, for example (/Applications/Blender/blender.app/Contents/Resources/2.79/python/bin/python3.5m, for version 2.79 on macOS)
  • Create a virtual environment under the desired location, like:
    # Need the following if Blender < 2.80
    /path/to/blender/bin/python -m ensurepip
    # Replace the path according to your system
    /path/to/blender/bin/python -m pip /path/to/virtualenv
        
  • Activate the virtual environment and install 3rd packages using pip
    # Replace the path and package name according to your system
    # For Windows system the path is under \path\to\virtualenv\Scripts\activate
    source /path/to/virtualenv/bin/activate
    python -m pip install 3rd-party-packages
        

Step 2: Modify the init script of virtual environment

The packages installed under the virtual environment needs to be found by the Blender-bundled python, which is done by modifying the PYTHONPATH environment variable.

Add the following line at the end of the activate script (/path/to/virtualenv/bin/activate):

export PYTHONPATH=$VIRTUAL_ENV/lib/pythonX.Y/site-packages/

Note the actual path may be different if you are using Windows. See comments below.

Reactivate the python vitrualenv will make the Blender-bundled python detect the 3rd-party package. Test this with a short script (for example named as test.py and the package is ase):

import sys
import bpy
print("Path to search python libraries", sys.path)
print("Python path is: ", bpy.__path__)

try:
    import ase
    print("ASE library in ", ase.__path__)
except ImportError:
    print("ASE not installed!")

Test this script with:

/path/to/blender -b -p test.py
@alchem0x2A
Copy link
Author

A couple of notes for others using this method:

I'm using blender 2.81 prerelease (bundles python 3.7.4) on Windows 10, and had to do this to create a working virtualenv:

$BLENDERTOP/2.81/python/bin/python -mpip install virtualenv
$BLENDERTOP/2.81/python/bin/python -mvirtualenv /path/to/virtualenv

Also, I'm on Windows, so my activate is in $VIRTUAL_ENV/Scripts/activate

And one more thing: I had to set PYTHONPATH to export PYTHONPATH=$VIRTUAL_ENV/Lib/site-packages.

But once I have that straightened out and activate the virtualenv, I can do pip install and blender -b --python foo.py and it finds my modules. Now if only python foo.py would work in the virtualenv! But this is workable.

Thanks for the comments and suggestion. I haven't used virtualenv on Windows yet so I'll just add your solutions to the gist.

Concerning the python scripts, your solution is nice!

@nlothian-ampintel
Copy link

For those coming across this, you might find that the answer https://blender.stackexchange.com/a/132279 gives you what you want (even if it isn't a proper virtualenv).

@sklein-ise
Copy link

Hey,
I am trying to get this to work for Blender 2.93 on a windows machine with anaconda and tried the following steps:

  1. activate virtual_env
  2. cd /path/to/blender/bin/
  3. python -mpip install virtualenv
  4. python -mvirtualenv /path/to/virtualenv
  5. editing activate script of virtual environment - inserting export PYTHONPATH=$VIRTUAL_ENV/Lib/site-packages

but when I now try to activate the virtual env and pip install something it throws me an error: No python at /path/to/blender/python.exe.
Does anyone else have the same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment