0
$\begingroup$

I have a Blender script that requires Pillow. The snippet shown below is the part that imports it. In one computer it works totally fine, in another computer it shows the following traceback:

Python: Traceback (most recent call last):
  File "\Multi_Material_Render.py", line 106, in <module>
    install_pillow()
ModuleNotFoundError: No module named 'PIL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "\Multi_Material_Render.py", line 109, in <module>

  File "\Multi_Material_Render.py", line 102, in install_pillow
    try:
  File "C:\Program Files\Blender Foundation\Blender 3.6\3.6\python\lib\subprocess.py", line 
369, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\\Program Files\\Blender Foundation\\Blender 
3.6\\3.6\\python\\bin\\python', '-m', 'pip', 'install', '--target', 'C:\\Program 
Files\\Blender Foundation\\Blender 3.6\\3.6\\python\\lib\\site-packages', 'Pillow']' returned 
non-zero exit status 3221225781.

both computers run Win 10. The problematic one is a fresh Win install. Blender is the only app installed. It was also working fine in that computer before reinstall. Why wont it work? I tried running as administrator. I also made sure internet is available.

import bpy
import os
import sys
import math
import subprocess
from bpy.props import StringProperty, EnumProperty
import pkg_resources
import platform


blender_python_path = os.path.join(sys.prefix, 'bin', 'python')
custom_install_path = os.path.join(sys.prefix, 'lib', 'site-packages')


def ensure_pip():
    try:
        subprocess.check_call([blender_python_path, '-m', 'ensurepip'])
    except subprocess.CalledProcessError:
        print("PIP is already available.")
    else:
        print("PIP has been installed.")
ensure_pip()
       
                                                                                               
def install_pillow():                                                                                                
                                                                                                                  
    subprocess.check_call([blender_python_path, "-m", "pip", "install", "--target", 
custom_install_path, "Pillow"])

  
try:
    from PIL import Image, ImageDraw, ImageFont
except ImportError:
    print("Pillow is not installed. Installing now...")
    install_pillow()
    print("Pillow has been installed.")                                                                               
    from PIL import Image, ImageDraw, ImageFont # Import Pillow after installation 
    
$\endgroup$
1
  • $\begingroup$ Hello ! I would check user rights. Make sure blender is run in admin mode and the user has write access in Blender's installation folder $\endgroup$
    – Gorgious
    Oct 31, 2023 at 8:57

2 Answers 2

0
$\begingroup$

I was able to fix it! I noticed that the python terminal (python.exe) was not running as standalone. All I needed to do was installing Microsoft Visual C++ modules and everything was fine after that.

$\endgroup$
0
$\begingroup$

It is missing vcruntime140.dll and maybe other DLLs that are in the blender.crt folder on Windows.

3221225781 is the STATUS_DLL_NOT_FOUND error.

env=env

You can add the blender.crt folder to os.environ['PATH'] and supply subprocess with env=os.environ.copy().

Environment Variables

Add blender.crt to PATH via the Windows means. Edit the system Environment variables

WindowsApps

The above methods may not work in C:\Program Files\WindowsApps\ for the Microsoft Store version of Blender without shell=True.

Microsoft Visual C++ Redistributable

Install the Microsoft Visual C++ Redistributable. Which puts the DLLs inside C:\Windows\System32\ that is in PATH and works for WindowsApps.

Programmatically

Although not recommended you can also use the pip's API in a separate -b --factory-startup Blender instance and after call importlib.invalidate_caches(). Works for WindowsApps.

from pip._internal import main
main(['install', '--upgrade', 'Pillow'])
$\endgroup$

You must log in to answer this question.

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