Short version: I need to install external modules for use within Blender, but can't for the reasons given below.
I'm on macOS 10.14.3 with Blender 2.91.2, which includes Python 3.7. I want to be able to use modules in Python that are not included in the Blender package. I've seen a number of posts here and on other sites about how to do this, but I can't get that version of Python to work from outside Blender and I can't do it from within Blender. This Mac is still running on original settings for Python, so the default Python for the Mac command line is 2.7. (I'd like to upgrade to the current OS version, but still haven't replaced some programs that won't work under the next update.)
I tried using the script given here to install pip:
import subprocess
import sys
import os
# path to python.exe
python_exe = os.path.join(sys.prefix, 'bin', 'python.exe')
# upgrade pip
subprocess.call([python_exe, "-m", "ensurepip"])
subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"])
# install required packages
subprocess.call([python_exe, "-m", "pip", "install", "package_name"])
I modified the executable name from "python.exe" to "python3.7m", which is what is used in the Mac version. I even verified the path name to make sure it was all correct. Whenever I try to run it, when I get to the first line under "# upgrade pip", I get a return code of -6. I tried variations and I simply cannot run a subprocess using the Python executable from within the Blender Python command line.
There are a number of links for installing pip by using the Python executable installed with Blender. Whenever I try to do that, I get:
Fatal Python error: initfsencoding: unable to load the file system codec File
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/init.py", line 123 raise CodecRegistryError,
^ SyntaxError: invalid syntaxCurrent thread 0x00000001183265c0 (most recent call first): Abort trap: 6
So to install pip, I have to be able to, in some way, run the Python executable included with Blender, but I can't run it. I'm assuming it needs settings that are different from my system environment variables, but I don't know what I'd have to change or how I'd pass that change on that instance of Python.
I am aware there is a pip module and I have tried to import it, but I can't find documentation on it online and all I get when I try "help('pip')" is this:
Help on package pip:
NAME pip
PACKAGE CONTENTS main _internal (package) _vendor (package)
VERSION 19.2.3
FILE /Applications/Blender.app/Contents/Resources/2.91/python/lib/python3.7/site-packages/pip/init.py
I also changed the python subdirectory inside the Blender directory to python-old, as suggested in some web pages. When I did that, Blender would not start. (I'm starting Blender from the command line so I can read Python error messages.)
What I need is a way to be able to run pip from the Python instance included with Blender so I can install Pillow (among other modules). But, if possible, I'd like to know what is going on that makes the Blender Python executable not work when I try it from the command line and how I could fix that.
supprocess.call
is effectively calling that executable... which IMO should bebpy.app.binary_path_python
orsys.executable
to use blenders python All errors above appear to point to yout python 2.7 install. $\endgroup$