Blender.Fun writes:
Hi Blender python enthusiasts,
today i want to show you how you can install any python modules in Blender so you can e.g. use "import pandas" in Blender scripts as well.
The link to my script is in the description of my yt video tutorial.
I hope you like it and may it useful to you.
Have a great day,
Blender.Fun
8 Comments
On Windows: run Blender as Administrator
You really shouldn't run apps as admin if you can avoid it..
True, but on Windows, Blender's site-packages directory isn't writeable otherwise (assuming Blender was installed using the msi installer).
Just add the ‘—user’ flag to the pip command (although this is not recommended either, at least you won’t introduce system vulnerabilities).
It is worrisome how this community produces “tutorials” for any script they found on the internet without any actual proficiency. This swamp of bad practice tutorials ultimately damages Blender and its reputation.
Unfortunately, at least the ppa Blender distribution I'm currently using on LInux does NOT respect the directory where the --user stuff goes.
To show all the possible paths from the command line you can use something like this (I wrote this from memory, so it may be slightly wrong):
blender -b --python-expr="import sys; [print(p) for p in sys.path]"
you can also run the following code inside the python shell or jupyter notebook blender kernel:
import sys
for p in sys.path:
print(p)
You can use pip's "--target" directive to install into one of these directories.
To get the usersite to resolve:
import site
import sys
usersite = site.getusersitepackages()
if usersite_str not in sys.path:
sys.path.append(usersite_str)
You can resolve the path using pathlib to make it more reliable on different OSes.
Adding the usersite to the Pythopath can lead to other problems. You might have installed stuff in there that clashes with other packages.
This is all a symptom of Blender not really being in touch with the larger Python community. Packaging isn't really a "solved" problem, but it's a lot more solved than it is within the Blender community.
Addons should be distributed via PyPi, using dependency management etc. This means less duplication of effort and a clear way to communicate package incompatibilities. All Blender installations should have a clear way to install not just addons but also generic Python modules (including binary wheels) without admin/root access.
in windows usage:
from sys import prefix
....
if isWindows():
return os.path.join(prefix, 'bin', 'python.exe')