Python Forum

Full Version: PIL not recognised in subprocess
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a script to show images using the the import "from PIL import Image, ImageTk" and it works fine when running the script by itself. I have Pillow installed.

However, when I call the script as a subprocess from another script it says "ModuleNotFound error No Moule named PIL"

What do I need to change to get the PIL recognised in the subprocess.

thanks any help would be much appreciated.
(Mar-05-2024, 04:32 AM)bobtbown Wrote: [ -> ]when I call the script as a subprocess from another script it says "ModuleNotFound error No Moule named PIL"
How do you launch the subprocess? Can you post the lines that launch the subprocess. Aren't you using a different Python interpreter?
As mention more info is needed,also what OS do you use.
Basic test that you can do from command line.
G:\div_code\po_env
λ python -c "import sys; print(sys.executable)"
C:\python312\python.exe

G:\div_code\po_env
λ python -c "import PIL; print(PIL.__file__)"
C:\python312\Lib\site-packages\PIL\__init__.py

G:\div_code\po_env
λ pip -V
pip 23.2.1 from C:\Python312\Lib\site-packages\pip (python 3.12)

G:\div_code\po_env
λ python open_tk.py
See that same Python interpreter is used,then i call it like this working.
tk_image_open.py is similar code as code as you have,a Tkinter app that use Pillow to show a image.
# open_tk.py
import subprocess
import sys

# Assuming tk_image_open.py is in the same directory
script_path = "G:/div_code/po_env/tk_image_open.py"

# Use sys.executable to ensure the same Python interpreter is used
subprocess.run([sys.executable, script_path])