# How to setup your system python for use with Blender

I found lots of material on how to make Blender use the system version of python. Essentially: delete the python folder of the install, and Blender looks for a system version, where on some systems you need to supply the path yourself as:

PYTHONPATH=/usr/lib/python3.7 ./blender --python-use-system-env


Official information from Blender docs found here.

What I cannot find is how to set up the system python to correspond perfectly with that of a given Blender version. Prefferably, I would like to do this with virtualenv.

The workflow I am interested in woud look something like this:

1. make a virtualenv that corresponds exactly to that of the Blender bundled python + dependencies/modules.
2. make Blender use this system python, of this virtualenv (if I have to issue a command like workon Blender2_91_2 before I start Blender, thats ok)
3. modify my virtualenv as need arise. Any change I make, I know i am likely to break things, which hopefully would be only other addons, which will be ok (its all only a virtualenv that I can play around with, since i know the proper bundled one)
• Renaming bundled python to say _python is a more reversible option. (It can then be used as version to create venv.I – batFINGER Feb 16 at 13:37
• @batFINGER, I tried renaming, and then calling the python binary from the bundled as '_python/bin/python3.7m -m venv python'. This created just a venv based on my system default python (3.6.9). I would just like to have a requirements.txt file, which is what I would create if I could activate the version that is bundled. However, it does not come with an activate script nor a requirements.txt. I also tried looking in the Blender main repo, but whatever tools are used to create the specific python version seems to be hidden out of my sight (just found cmake stuff for using system python) – Stefan Karlsson Feb 18 at 9:32
• @batFINGER, Its a little difficult to understand you previous remark, but thanks for the help :). When I try to use my system based python (for both 3.6.9 and 3.7.9 by starting from venv) I get the same error: no module encodings. When I try to use the venv that I derived from the Blender bundled python, same error: no module encodings. I see this module inside of the bundled Python, but not in site-packages. I could copy-paste it, but that would go against what I want to achieve. – Stefan Karlsson Feb 18 at 18:24
• I went ahead and built Blender from the code using the "install_deps.sh" script (instead of the svn approach). The python folder does appear after completion but trying to decode the script, I ended up once more hitting into cmake: "build_files/build_environment/cmake/python.cmake". Decoding cmake is a painful, and commonly fruitless experience. I also grepped through the entire repo for any mention of the module "encodings", and found nothing. – Stefan Karlsson Feb 18 at 18:52

Edit.. If having encodings hassles try virtualenv

After installing python 3.77 to /usr/local/ from a tarball, thought I would create a new python venv for blender. Using /usr/local/bin/python3.7 -m venv foo --copies I ran into the no module named encodings error, and found no way to alleviate the situation.

Thought I'd give the somewhat deprecated virtualenv a try, and bingo, blender ran straight up.

/usr/bin/virtualenv -p /usr/local/bin/python3.7 --always-copy foo


creating a copy of the python to venv and from it blender runs without any extra steps required.

A way for Ubuntu

Having horrible issues building blender currently, so have gone thru the motions of setting up virtual environments for freshly downloaded 2.91.2 and 2.92.0

Have both in vers folder

Note commands shown below are <prompt> $<command> shitbox:~/blender/vers$ ls
blender-2.91.2-linux64  blender-2.92.0rc-linux64


renamed the python folder in both from python to _python

shitbox:~/blender/vers/blender-2.92.0rc-linux64/2.92$mv python _python  Now set up a virtual environment named "bar" and changed into that directory shitbox:~/blender$$~/blender/vers/blender-2.92.0rc-linux64/2.92/_python/bin/python3.7m -m venv bar --copies shitbox:~/blender$$ cd bar  next have made link copies of blender's distribution python lib to venv lib shitbox:~/blender/bar$ cp -as /home/batfinger/blender/vers/blender-2.92.0rc-linux64/2.92/_python/lib .


Created symlink to blender into the venv's bin.

shitbox:~/blender/bar$ln -s /home/batfinger/blender/vers/blender-2.92.0rc-linux64/blender bin/blender  activate the venv shitbox:~/blender/bar$ source bin/activate


and start blender telling it to use this venv

shitbox:~/blender/bar$$BLENDER_SYSTEM_PYTHON="$$VIRTUAL_ENV" blender


The environment variable can be set in the activate script in your venv's bin folder, however on Ubuntu I am using direnv which automagically activates the environment when entering the folder using an .envrc file. So I have stuck it in there

export EDITOR=nvim
source $$(pwd)/bin/activate export BLENDER_SYSTEM_PYTHON="$$VIRTUAL_ENV"

echo "Blender 2.92"


and similarly for 2.91 in "foo"

(foo) shitbox:~/blender/foo$blender Read prefs: /home/batfinger/.config/blender/2.91/config/userpref.blend found bundled python: /home/batfinger/blender/foo  Can install in the venv with pip pip install matplotlib  and bingo in blender >>> import sys >>> sys.executable '/home/batfinger/blender/foo/bin/python3.7m' >>> import matplotlib >>> matplotlib <module 'matplotlib' from '/home/batfinger/blender/foo/lib/python3.7/site-packages/matplotlib/__init__.py'>  Notes. Above is demonstrating how to use the python that ships with blender to make a venv. Would be very inclined to download and build the matching version Python3.7.7 and use this as the basis for a venv. (It's my experience that python builds pretty well hassle free) It's been a while since I downloaded an official build of blender and was a little surprised to see both versions above using python 3.7.7. (Have been building against 3.8.2 since blender 2.7, last run of install_deps.sh to build was using 3.9.1 .. miss not building with the "latest" .. don't miss when it doesn't work) Since both venvs use same python version it feels a little overkill to install scipy, matplotlib, pynvim, plumbum and other 3rd party modules I use alot for each venv. In the past any module I wish to have available for all instances I have used pip install pynvim --user  which is unavailable in venvs as setup above, unless python --system-site-packages was used creating venv, or in hindsight edit pyvenv.cfg to have include-system-site-packages = true or add a path to python path in .envrc with export PYTHONPATH="$HOME/.local/lib/python3.7/site-packages"


will make that path available to python in your venv, but not in blender..

A quick way around this is make a script to add paths to blender

addpaths.py

import sys
sys.path.append("/home/batfinger/.local/lib/python3.7/site-packages")


and start blender with

blender -P addpaths.py


TODO write a script to automate the above.

• Aaaaaargh! 'Thank' you. This is the sort of stuff that drives me nuts, and means days of almost-random hunting around, and unwanted learning of sysadmin, before I can get to any coding at all. I've been trying for weeks, on and off, now, to get a library compiled against a more recent Python to be callable from Blender. I guess I should ask a Q of my own, but hard to know what's out of bounds. – Robin Betts Feb 19 at 15:55
• Thanks Mr Betts, Still needs some testing and fixin' Building blender from source is a sanity test for me. Stopped daily / weekly building a few months back, tried yesterday .to see some new goodies.. back in nappies. Getting pre-built blender is no issue, bpy as a python module is. – batFINGER Feb 19 at 16:17
• @batFINGER, cant wait to get to try this out! Thank you! Unfortunately, cant get at it until I am feeling better from this cold, surely will accept the answer. Its beautiful :) – Stefan Karlsson Feb 19 at 17:22
• Success! And only by the help of this post. However, it is not self contained as an answer. Quite some tweaking required. Since Matplotlib is one of the first things I wanted working, there was quite a few things extra to be done, including: building pyton3.7.10, instead of 3.7.7, installing lots of extra dependencies, modifying matplot lib default backend. If I have time and energy, I will make a first version of that script mentioned above. Linking it here, even though its not finished would be enough to accept the answer. Until then, I wish I could upvote it more :) – Stefan Karlsson Feb 21 at 13:47