1
$\begingroup$

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 syntax

Current 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.

$\endgroup$
7
  • $\begingroup$ I didnt read your Q carefully, but I had an issue with pip too ... this might help blender.stackexchange.com/q/211697/2214 and what I read - don't uninstall Macs python 2.7 version instead you should create pyenvi if you want another regular installation, but for me just installation of modules for my current Blender app was OK (I don't have an experiences so I didnt want to mess it up :) $\endgroup$
    – vklidu
    Commented Mar 1, 2021 at 8:42
  • $\begingroup$ Possible dupe: blender.stackexchange.com/questions/56011/… Also ran thru this recently to use bundled python to create a venv for blender $\endgroup$
    – batFINGER
    Commented Mar 1, 2021 at 13:32
  • $\begingroup$ @batFINGER: It's not a dupe. The answer for that question requires running the Python executable installed by Blender and this questions specifically states that I can't do that in this install. I got excited about the idea of creating a venv in the 2nd, but that all seems to require running the executable first - but I'm studying that. I'd love to find an answer there. I'm wondering if I should change the question to just focus on finding a way to run the executable from the command line. If I get that, installing pip would be easy. $\endgroup$
    – Tango
    Commented Mar 1, 2021 at 15:51
  • $\begingroup$ Now I'm confused. supprocess.call is effectively calling that executable... which IMO should be bpy.app.binary_path_python or sys.executable to use blenders python All errors above appear to point to yout python 2.7 install. $\endgroup$
    – batFINGER
    Commented Mar 1, 2021 at 16:07
  • $\begingroup$ @batFINGER The problem is that before fixing that, I have a bigger problem. The executable in /Applications/Blender.app/Contents/Resources/2.91/python/bin/python3.7m won't run, period. It won't run from the command line and gives an abort error of 6. When I try it with subprocess.call, I get a -6 return code, which sounds like it's the same issue. So I have to get the executable to run first. That's why I'm thinking of rewriting the question or deleting it and replacing it - the bottom line issue is the Python executable included with Blender won't run in my system environment. $\endgroup$
    – Tango
    Commented Mar 1, 2021 at 16:17

1 Answer 1

1
$\begingroup$

I had to go through several other issues to pin this down, including another question on stack Overflow, where I was not getting much help. I'll include what I can here. That'll be more than most people want to read, but if you have this problem, skim it because you might find helpful stuff in here.

The bottom line is that I was setting PYTHONPATH in .bash_profile. When I bought this current iMac, Python 2.7 was the standard on an install. At some point (and I had forgotten this), Apple changed to 3.x as the standard. That crashed some of my scripts, so I set PYTHONPATH (again, in .bash_profile) to point to all the 2.7 libraries, so my scripts would continue to look there.

I finally figured out that any 3.x version of Python was crashing on my system and 2.x was running. I thought that was probably a configuration issue and I'd have to find some config file to fix somewhere, but, all along, it was in the .bash_profile. Once I changed all the paths in there to point to 3.7 instead of 2.7, everything started working fine. Python 3 executables were working fine and even the 2.x ones were still working. (And more of my scripts than I expected worked fine under 3.x.)

Once I got that working, I wrote a batch file I can run in bash to set PYTHONPATH to point to the directories Blender uses when it runs Python in its own environment then run the Blender instance of Python. It works fine and that let me follow instructions in a number of places to use ensurepip to install pip and then to install Pillow.

$\endgroup$

You must log in to answer this question.

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