HF1
HF1

Reputation: 475

No module named 'PIL'

I'm running into an error where when I try

from PIL import Image, ImageFilter

in a Python file I get an error stating ModuleNotFoundError: No module named 'PIL'.

So far I've tried uninstalling/reinstalling both PIL and Pillow, along with just doing import Image, but the error keeps on occurring and I have no idea why. All the solutions I've found so far have had no effect on my issue.

I'm running Python 3.5 on Ubuntu 16.04

Upvotes: 22

Views: 54045

Answers (7)

Will
Will

Reputation: 241

Had the same problem before and I tried:

pip install pillow 
pip install image 

import PIL --> still did not work

then I found out that it was installed as pil

c:\python36\lib\site-packages\pil

import pil

pil.__version__

'5.1.0'

Upvotes: 14

Amnuaymek Chom
Amnuaymek Chom

Reputation: 9

I solve this problem with venv.

  1. Clean up the project so that only the program file remains.
  2. Create a virtual environment using the following command:
    python -m venv venv-name
    
  3. Activate the virtual environment by running the command:
    venv-name\Scripts\activate
    
  4. Create a file named requirements.txt containing all the required packages, for example:
    pillow
    
  5. Install the required packages by running the command:
    pip install -r requirements.txt
    
  6. Voila, it works!

Upvotes: 0

Eric Jiang
Eric Jiang

Reputation: 11

This subject has been a while, but I would like to share what I have encountered.

I used to have Pillow installed and it worked well. But today when I was doing something, it started to give me this error "ModuleNotFoundError: No module named 'PIL'". I tried all the methods in this subject, but the problem was still there.

So I went to Pillow website trying to use the official installation guide. After I enter "python3 -m pip install --upgrade pip", system told me I don't have pip installed. Then I realized the python it referred to is the one in my mingw64 library (C:\msys64\mingw64\bin), as I installed it yesterday for C++ compilation and it was added to my system environment path.

After removing the mingw64 bin library from system environment and re-installing Pillow, "from PIL import image" works perfectly.

Upvotes: 1

Ratul Doley
Ratul Doley

Reputation: 509

I am using ubuntu 18.04 and I was facing exactly same issue after installing pillow following the official documentation. I tried all hacks shared by other users but no solution. But the problem got immediately resolved when I installed python-pil using the following command:

sudo apt-get install python3-pil

Upvotes: 4

Leang TongLeng
Leang TongLeng

Reputation: 41

I solved the issue with the command python3 -m pip install Pillow.

Upvotes: 4

Michael Uhlenberg
Michael Uhlenberg

Reputation: 41

In my case the problem had to do with virtual environments.

The python program ran in a virtual environment, but I called pip install Pillow from a normal command prompt. When I ran the program in a non-virtual environment, from PIL import Image worked.

It also worked when I called venv/scripts/activate before calling pip install Pillow. So apparently PIL is not found when installed in the python root but the program runs in a virtual environment.

Upvotes: 4

HF1
HF1

Reputation: 475

Alright, I found a fix

To fix the issue, I uninstalled PIL and Pillow through sudo pip3 uninstall pillow and sudo apt-get purge python3-pil. I then restarted and then used sudo -H pip3 install pillow to reinstall Pillow

The only step I was missing before was rebooting, and not reinstalling PIL afterwards.

It seems to have worked without any issues so far.

Upvotes: 8

Related Questions