ModuleNotFoundError: No module named 'PIL'

#! /usr/bin/python3
# On 10/4/22 I installed version Python 3.10.7
# Does not work

# Traceback (most recent call last):
  # File "/home/andy/Python/png2jpg.py", line 10, in <module>
    # from PIL import Image
# ModuleNotFoundError: No module named 'PIL'
import math 

import os
import sys
from PIL import Image

if len(sys.argv) > 1:
    if os.path.exists(sys.argv[1]):
        im = Image.open(sys.argv[1])
        target_name = sys.argv[1] + ".jpg"
        rgb_im = im.convert('RGB')
        rgb_im.save(target_name)
        print("Saved as " + target_name)
    else:
        print(sys.argv[1] + " not found")
else:
    print("Usage: convert2jpg.py <file>")

I installed Pillow.

pip3 install Pillow

1 Like

What version of Python are you using to run the script? You have to install Pillow for each and every version of Python you use.

Python 3.10.7

How do I install Pillow for this version?

If you run that Python using python3 command then use the same command with option -m pip to invoke pip for that version. Example:

python3 -m pip install Pillow

That assumes that python3 is the same as python3.10, it may be some other version.

You may need to run python3.10 -m pip install Pillow.

which python3 may also give you some useful information.

ERROR: Could not find a version that satisfies the requirement Pillow (from versions: none)
ERROR: No matching distribution found for Pillow
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org’, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)) - skipping
WARNING: There was an error checking the latest version of pip.

which python3
/usr/local/bin/python3

I have a second Ubuntu Mate installation that is 20.04.

My png2jpg script does work on it.

I did not need to install PIP either. :slight_smile:

Which suggests that /usr/local/bin/python3 does not have the ssl
module. How was it installed?

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes
Which suggests that `/usr/local/bin/python3` does not have the `ssl`
module. How was it installed?

I do not remember how it was installed. I hope I can get it running in 18.04.

1 Like

What about which -a python3? If there are several, try each.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

I tried it and no luck.

1 Like

I discovered this. Does it help in resolving my problem?

Modules are in /home/andy/.local/lib/python2.7/site-packages/

Thanks.

1 Like

Where did this message come from?

These will be modules for Python 2.7, and you’re using Python 3.

What was the listing of installed python3 commands?

For each such, does:

 python3 -m pip install Pillow

succeed or fail, replacing python3 above with the full path to the
various python3 executables listed by which -a python3?

Cheers,
Cameron Simpson cs@cskk.id.au

It just occurred to me that Ubuntu systems install some normal components of Python 3 as distinct packages, eg python-tk for the Tkinter modules.

Try installing python3-openssl, eg:

# apt-get install python3-openssl

and then see if pip’s behaviour improves.

Thanks for your help. I installed it, but I get the same error.

I wish I could find someone running Ubuntu Mate 18.04 who is using Python.

This is a nice script I found that lets you paste snippets.
It runs because it uses no modules.

#!/usr/bin/env python3
#
# /home/andy/.config/snippet_paste/ is where text to be inserted reside.
# Use Alt P to activate

import os
import subprocess

home = os.environ["HOME"]
directory = home+"/.config/snippet_paste"
if not os.path.exists(directory):
    os.mkdir(directory)
# create file list with snippets
files = [
    directory+"/"+item for item in os.listdir(directory) \
         if not item.endswith("~") and not item.startswith(".")
    ]
# create string list
strings = []
for file in files:
    with open(file) as src:
        strings.append(src.read())
# create list to display in option menu
list_items = ["manage snippets"]+[
    (str(i+1)+". "+strings[i].replace("\n", " ").replace\
     ('"', "'")[:20]+"..") for i in range(len(strings))
    ]
# define (zenity) option menu
test= 'zenity --list '+'"'+('" "')\
      .join(list_items)+'"'\
      +' --column="text fragments" --title="Paste snippets"'
# process user input
try:
    choice = subprocess.check_output(["/bin/bash", "-c", test]).decode("utf-8")
    if "manage snippets" in choice:
        subprocess.call(["seamonkey", directory])
    else:
        i = int(choice[:choice.find(".")])
        # copy the content of corresponding snippet
        copy = "xclip -in -selection c "+"'"+files[i-1]+"'"
        subprocess.call(["/bin/bash", "-c", copy])
        # paste into open frontmost file
        paste = "xdotool key Control_L+v"
        subprocess.Popen(["/bin/bash", "-c", paste])
except Exception:
    pass

Help me Obiwan Kenobi, you are my only hope. :slight_smile:

Thanks for your help. I installed it, but I get the same error.

From every python3 executable? eg does:

 /usr/bin/python3 -m pip install Pillow

succeed or fail? A full transcript of the output would be helpful.

Ubuntu provides a python3-pillow package. Have you tried:

 apt-get install python3-pillow

I wish I could find someone running Ubuntu Mate 18.04 who is using Python.

I’m running 21.04 here unfortunately, not 18.04. I do have an 18.04 box
on the shelf I could set up again, and I suppose I could make a VM.

This is a nice script I found that lets you paste snippets.
It runs because it uses no modules.

“os” and “subprocess” are modules, they just come presupplied. Normally
“ssl” is also presupplied.

Cheers,
Cameron Simpson cs@cskk.id.au

python3.6 -m pip install --upgrade pip

python3.6 -m pip install --upgrade Pillow 

#! /usr/bin/python3.6

# Use python3.6 [name of python script]
# Much help from ironfoot and karel
 
import os
import sys
from PIL import Image
 
if len(sys.argv) > 1:
    if os.path.exists(sys.argv[1]):
        im = Image.open(sys.argv[1])
        target_name = sys.argv[1] + ".jpg"
        rgb_im = im.convert('RGB')
        rgb_im.save(target_name)
        print("Saved as " + target_name)
    else:
        print(sys.argv[1] + " not found")
else:
    print("Usage: convert2jpg.py <file>")
    
python3.6 -m pip install --upgrade pip
python3.6 -m pip install --upgrade Pillow

Did these succeed? I don’t see a transcript eg shell prompt and
resulting output.

Example here:

 [~/hg/css-ebooks(hg:ebooks)]fleet2*> /usr/local/bin/python3.8 -m pip install --upgrade Pillow
 DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
 Requirement already satisfied: Pillow in 
 /Users/cameron/Library/Python/3.8/lib/python/site-packages (8.3.1)
 Collecting Pillow
   Using cached Pillow-9.2.0-cp38-cp38-macosx_10_10_x86_64.whl (3.1 MB)
 Installing collected packages: Pillow
   Attempting uninstall: Pillow
     Found existing installation: Pillow 8.3.1
     Uninstalling Pillow-8.3.1:
       Successfully uninstalled Pillow-8.3.1
   DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
 DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
 Successfully installed Pillow-9.2.0
 WARNING: You are using pip version 21.2.4; however, version 22.2.2 is available.
 You should consider upgrading via the '/usr/local/opt/python@3.8/bin/python3.8 -m pip install --upgrade pip' command.

Ignore the deprecation warnings, they’re an artifact of the local
environment.

#! /usr/bin/python3.6

This will indeed run the script using /usr/bin/python3.6 if you invoke
the script directly as a command.

But you can run it with any Python interpret to test its behaviour with
that interpreter, eg:

 /usr/bin/python3.6 your-script.py

Adjust to suit.

Cheers,
Cameron Simpson cs@cskk.id.au

Yes, it succeeds.

python3.6 png2jpg.py Dust_Collector.png
Saved as Dust_Collector.png.jpg

Eventually I want to fix it so it outputs Dust_Collector.jpg.

if len(sys.argv) > 1:
    if os.path.exists(sys.argv[1]):
        im = Image.open(sys.argv[1])
# Backspace 4 spaces  and add .jpg    
  target_name = "sys.argv[1] - 4" + ".jpg"
        rgb_im = im.convert('RGB')
        rgb_im.save(target_name)
        print("Saved as " + target_name)
    else:
        print(sys.argv[1] + " not found")
else:
    print("Usage: convert2jpg.py <file>")

This does save it with .jpg but the file name is mangled.

Saved as sys.argv[1] - 4.jpg