I have experimented quite a bit with extending the modules that come with the blender bundled python. It is confusing to me, and seems not well documented anywhere how it actually works.
A folder called Python exists in the Blender folder, and it is described in numerous places as the bundled python. In it, you can find a structure very similar to that of a Python venv. That is, you can create a virtual python environment by using the venv command as:
python3.7m -m venv myVenvFolderPath --copies
This follows regular python convention. Since the python folder contains some other dependencies you need to copy them as well, in order to recreate a venv that is as similar to that of the bundled python that comes with Blender (see this explanation for details)
Furthermore, this binary (python3.7m in the blender python folder) is referred to online virtually everywhere as the bundled python interpreter.
however, i doubt that it really is. If you delete this binary, Blender will still run very happily. In the python console inside of blender, you can then run:
import sys sys.executable
and you will find that Blender will have just found some other python 3.7 binary, whatever it can find in the path of your system. Blender runs perfectly fine with this, even if the python binary that it found was actually another version. On my system, Blender finds the "Python3.7m" version that is in the path, that is actually version 3.7.10, and not 3.7.7 version of Python. Again, that is all fine with Blender. I also have built Python from source, and have fed the python binary (as a venv) to blender, using a wide variety of build options. Inside of blender, there is never any problems that I have encountered, and mysteriously Blender always reports version 3.7.7 in the startup of the python console inside of blender.
Right now, I am inclined to guess that the python binary that comes with Blender is actually not the interpreter at all, but that the interpreter is either statically linked into blender, or available as a dynamic library. If someone could verify that, it would really illuminate me, thanks.
Second part of this question, which I think is connected to the first part, is why I am getting very odd errors and crashes of Blender, when i add certain modules to my python environment. This happens regardless of what version of python I am using to create the venv, or even doing the pip install.
This happens when i try to run matplotlib with any backend that is interactive, except for the "webagg" option. Since the built-in python interpreter (which is located somewhere mysteriously) does not have tkinter built in, I have tried using QT4 and cairo as visual backend frameworks. Using any of them will properly bring up the matplotlib.pyplot figures I want in interactive mode, but afterwards Blender becomes unstable and will always crash on me after some time. The error is always the same, a full immediate crash due to segm fault, and the output in the terminal reading:
ERROR (gpu.shader): GPU_material_compile FragShader: | 10412 | const vec3 cons26 = vec3(0,000000000000, 0,000000000000, 0,000000000000); | | Error: C1068: too much data in type constructor | 10414 | const float cons28 = float(0,000000000000); | | Error: C1068: too much data in type constructor | 10415 | const float cons29 = float(1,000000000000); | | Error: C1068: too much data in type constructor | 10416 | const float cons30 = float(1,000000000000); | | Error: C1068: too much data in type constructor | 10418 | const vec3 cons35 = vec3(0,000000000000, 0,000000000000, 0,000000000000); | | Error: C1068: too much data in type constructor
All interactive backends that "work" yields this instability, with the exception of "webagg", which requires the tornado webserver to be installed with pip. This one works great, using the web-browser for the plots, except that I need to go to the terminal to issue a ctrl-c, in order not to freeze up Blender.
I am working in Ubuntu 18.04, with Blender 2.91.2. I am on a laptop, Lenovo thinkpad P1, gen 2, with a Quadro T1000 graphics card