4
$\begingroup$

I have a bunch of modules that I had previously installed using pip to my system python 2.7. What do I need to do in order to be able to import this module in the blender python console, as well as within my python scripts in blender? (The module in question is called ProDy).

EDIT: I installed pyenv and installed the same version of python as my blender version (3.5.3) uses through it. I then made this version active, and installed the packages I need with pip3 install. I can load the packages in terminal, but not in the blender python console. What am I missing?

$\endgroup$
6
  • $\begingroup$ You'll have to upgrade to Python 3 first. $\endgroup$
    – dr. Sybren
    Commented May 11, 2018 at 6:49
  • $\begingroup$ So if I python 3 and the modules for python 3, that would work in blender? $\endgroup$ Commented May 11, 2018 at 13:55
  • $\begingroup$ @dr.Sybren I tried this, please see the updates in the post. $\endgroup$ Commented May 11, 2018 at 16:52
  • 1
    $\begingroup$ I answered a similar question not too long ago about installing scipy. The instructions should work for you too if you're using Windows. $\endgroup$
    – doakey3
    Commented May 11, 2018 at 16:56
  • $\begingroup$ @doakey3 Oh cool, thanks! I'm using mac, but I guess it should still be the same. One question though: would I have to do this every time I launch blender? $\endgroup$ Commented May 11, 2018 at 16:58

2 Answers 2

7
$\begingroup$

Update for the Snap-released Blender versions (e.g. 2.93)

In general, and for good reasons, it is not possible to modify snap installations.

This means that the procedure below will first try to install pip and the packages at the default location inside snap, but will silently fail due to the following error:

Defaulting to user installation because normal site-packages is not writeable

Then, it will install the dependencies in some other "default" location allowed (in my Ubuntu20.04 this was ~/.local/lib/python3.9/site-packages), and this is likely a place that Blender is not expecting. As a consequence, installation will be "successful" but Blender will still be unable to find the dependencies.

To diagnose this, open the Blender Python console and check sys.path to see which paths is Blender expecting, and pip show <PACKAGE> to see where has the package been installed. If the path is not in the expected list, it won't be found.

There are a few ideas to fix this:

  • Force our way into snap through the OS and install files there (bad idea and likely to be a headache)
  • Use subprocess to install into snap using Blender's Python, as discussed here
  • Tell Blender where to look for our new files

From all 3, I personally think that option 3 is the simplest and most versatile one. We can see that Blender's Python options now include the following flag:

--python-use-system-env: Allow Python to use system environment variables such as PYTHONPATH and the user site-packages directory.

In my case, the solution was simply to install pip and the packages as before, and run Blender as before, but adding the --python-use-system-env flag when running Blender, to tell that we want to look for dependencies outside snap.

This worked like a charm for my system, but if it doesn't, an alternative is to add the desired path to sys.path before importing (e.g. by appending the path string or by updating $PYTHONPATH).

Hope this helps!


Original solution

For pip-installable dependencies (like PyPI or wheels), all you need to do is the following:

  1. Locate your Blender's Python binary path, let's call it <BPYTHON> (in my case 2.80/python/bin/python3.7m at the Blender installation). The path can be retrieved from blender itself, by running the following inside the Blender Python terminal:
import sys
sys.executable
# Alternatively run the following one-liner, if the version is new enough
blender -b --python-expr "import sys; print(sys.executable)"
  1. Run the following to enable pip operations in bpython:
<BPYTHON> -m ensurepip
<BPYTHON> -m pip install --upgrade pip
  1. Now any time you want to install a package simply call pip from bpython:
<BPYTHON> -m pip install <YOUR_PACKAGE>  # no --user needed
<BPYTHON> -m pip install <PATH/TO/WHEEL>.whl  # also works with wheels as expected

As usual, you just have to install a package once and uninstalling works the analogous way.

Blender's Python has its own environment, so this procedure will install the dependencies there (in my case the 2.80/python folder). AFAIK this will work irrespectively of your OS, system's Python version and Blender location so I found this to be most convenient. You can encounter some issues if installing cython-related packages like tkinter but this can also be fixed as I did here.

Let me know if this works out!
Cheers,
Andres

$\endgroup$
2
  • $\begingroup$ Cool! thanks for the info. How is that? I checked Blender's sys.path but didn't find any clear pointers $\endgroup$
    – fr_andres
    Commented May 13, 2019 at 13:45
  • $\begingroup$ Works like a charm in blender 3.3.3, in windows just add to your shortcut property in target : <your install path of blender>\blender.exe --python-use-system-env $\endgroup$
    – cscholl
    Commented Feb 10, 2023 at 22:37
1
$\begingroup$

Just figured I would add my own solution based on @fr_andres to make using the snap based Blender version a bit cleaner:

In Blender 3.4, sys.path includes a few additional paths in the home directory:

  • ${HOME}/.config/blender/3.4/addons
  • ${HOME}/.config/blender/3.4/addons/modules

I'm not sure what the original intent behind the modules directory is, but one clever use of it is to install all extra Python modules there. Additionally, pip has some additional support for this using the --target= flag:

pip3 install --target=${HOME}/.config/blender/3.4/scripts/addons/modules matplotlib scipy

At least for me, this turned out to be the simplest approach.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .