ImportError: No module named PIL - python

I use this command in the shell to install PIL:
easy_install PIL
then I run python and type this: import PIL. But I get this error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named PIL
I've never had such problem, what do you think?

In shell, run:
pip install Pillow
Attention: PIL is deprecated, and pillow is the successor.

On some installs of PIL, you must do
import Image
instead of import PIL (PIL is in fact not always imported this way). Since import Image works for you, this means that you have in fact installed PIL.
Having a different name for the library and the Python module is unusual, but this is what was chosen for (some versions of) PIL.
You can get more information about how to use this module from the official tutorial.
PS: In fact, on some installs, import PIL does work, which adds to the confusion. This is confirmed by an example from the documentation, as #JanneKarila found out, and also by some more recent versions of the MacPorts PIL package (1.1.7).

On a different note, I can highly recommend the use of Pillow which is backwards compatible with PIL and is better maintained/will work on newer systems.
When that is installed you can do
import PIL
or
from PIL import Image
etc..

At first install Pillow with
pip install Pillow
or as follows
c:\Python35>python -m pip install Pillow
Then in python code you may call
from PIL import Image
"Pillow is a fork of PIL, the Python Imaging Library, which is no longer maintained. However, to maintain backwards compatibility, the old module name is used." From pillow installed, but "no module named pillow" - python2.7 - Windows 7 - python -m install pillow

This worked for me on Ubuntu 16.04:
sudo apt-get install python-imaging
I found this on Wikibooks after searching for about half an hour.

Sometimes I get this type of error running a Unitest in python. The solution is to uninstall and install the same package on your virtual environment.
Using this commands:
pip uninstall PIL
and
pip install PIL
If for any reason you get an error, add sudo at the beginning of the command and after hitting enter type your password.

you have to install Image and pillow with your python package.
type
python -m pip install image
or run command prompt (in windows), then navigate to the scripts folder
cd C:\Python27\Scripts
then run below command
pip install image

On windows 10 I managed to get there with:
cd "C:\Users\<your username>\AppData\Local\Programs\Python\Python37-32"
python -m pip install --upgrade pip <-- upgrading from 10.something to 19.2.2.
pip3 uninstall pillow
pip3 uninstall PIL
pip3 install image
after which in python (python 3.7 in my case) this works fine...
import PIL
from PIL import image

I used:
pip install Pillow
and pip installed PIL in Lib\site-packages. When I moved PIL to Lib everything worked fine. I'm on Windows 10.

Install Specific Version:
pip install Pillow
Upgrade Pillow
sudo pip3 install --upgrade Pillow
Getting Dependency Error in Window 10
Use code: easy_install instead of pip install
easy_install Pillow
Upgrade using easy install
sudo easy_install --upgrade Pillow
On OSX System to install Module:
Use code: brew install instead of pip install
brew install Pillow
Without Using Pip :
sudo apt-get install -y Pillow
On CentOS7 or Linux Fedora:
yum -y install Pillow
Or on Fedora try
sudo dnf install Pillow
Command if Homebrew screws up your path on macOS:
python -m pip install Pillow
For Python3 MacOs Homebrew screws
python3 -m pip install Pillow
Verify module from list MacOs
pip freeze | grep Pillow
For Execute on Anaconda as your python package manager
conda install -c anaconda Pillow

On windows, try checking the path to the location of the PIL library. On my system, I noticed the path was
\Python26\Lib\site-packages\pil instead of \Python26\Lib\site-packages\PIL
after renaming the pil folder to PIL, I was able to load the PIL module.

if you use anaconda:
conda install pillow

You will need to install Image and pillow with your python package.
Rest assured, the command line will take care of everything for you.
Hit
python -m pip install image

instead of PIL use Pillow it works
easy_install Pillow
or
pip install Pillow

The cleanest way to resolve this issue is by following below steps.
Step 1: Uninstall the PIL package.
pip uninstall PIL
Step 2: Install the Pillow using pip as shown below on windows operating systems. For other environments checkout the article No module named PIL
On Windows
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
Step 3: The most crucial class in the Python Imaging Library is the Image class, and you can import this as shown below.
from PIL import Image
im = Image.open("myimage.jpg")
If successful, this function returns an Image object. You can now use instance attributes to examine the file contents:
print(im.format, im.size, im.mode)
#Output: PPM (512, 512) RGB

I had the same issue on windows 10 and this worked for me:
pip install Pillow

pip(3) uninstall Pillow
pip(3) uninstall PIL
pip(3) install Pillow

This worked for me on Ubuntu 20.04:
pip install image
And in script:
import image

use pil instead of PIL
from pil import Image

On Windows, you need to download it and install the .exe
https://pypi.python.org/pypi/Pillow/2.7.0

I used conda-forge to install pillow version 5, and that seemed to work for me:
conda install --channel conda-forge pillow=5
the normal conda install pillow did NOT work for me.

I had the same problem and i fixed it by checking what version pip (pip3 --version) is, then realizing I'm typing python<uncorrect version> filename.py instead of python<correct version> filename.py

I used :
from pil import Image
instead of
from PIL import Image
and it worked for me fine
wish you bests

I had the same issue while importing PIL and further importing the ImageTk and Image modules. I also tried installing PIL directly through pip. but not success could be achieved. As in between it has been suggested that PIL has been deprectaed, thus, tried to install pillow through pip only. pillow was successfully installed, further, the PIL package was made under the path : python27/Lib/site-packages/.
Now both Image and ImageTk could be imported.

I recently installed Leap. I Tried openshot and it didn't start. So came here and found a suggestion to start from the Terminal to see if there were any error.
The error I had was error missing mlt. So I installed the python-mlt module from Yast and imported it, tried to start but next openshot said missing pil.
I Followed the Pillow suggestion to install because Yast couldn't find any pil and imported pil. That went ok but did not start and showed Error missing goocanvas.
The I installed goocanvas with Yast, imported it in python, and Openshot fired up !!
With a lot of errors in the terminal like missing Vimeoclient and lots of attributeerrors. Well, will see if it is of any influence working with it.

Python 3.8 on Windows 10. A combination of the answers worked for me. See below for a standalone working example. The commented out lines should be executed in the command line.
import requests
import matplotlib.pyplot as plt
# pip uninstall pillow
# pip uninstall PIL
# pip install image
from PIL import Image
url = "https://images.homedepot-static.com/productImages/007164ea-d47e-4f66-8d8c-fd9f621984a2/svn/architectural-mailboxes-house-letters-numbers-3585b-5-64_1000.jpg"
response = requests.get(url, stream=True)
img = Image.open(response.raw)
plt.imshow(img)
plt.show()

I had the same issue and tried many of the solutions listed above.
I then remembered that I have multiple versions of Python installed AND I use the PyCharm IDE (which is where I was getting this error message), so the solution in my case was:
In PyCharm:
go to File>Settings>Project>Python Interpreter
click "+" (install)
locate Pillow from the list and install it
Hope this helps anyone who may be in a similar situation!

I found an easier solution. Use a virtual environment.
pip install Pillow
from PIL import Image
This works for me on a macOS

According to the official websiteInstall Pillow, you may want to try this:
go to Terminal and run:
python3 -m pip install --upgrade pip
Then Run on
source ~/.bash_profile

You are probably missing the python headers to build pil. If you're using ubuntu or the likes it'll be something like
apt-get install python-dev

Related

I installed Pillow but i get "ModuleNotFoundError: No module named 'PIL'"

When I try to use from PIL import ImageGrab it gives me this error:
ModuleNotFoundError: No module named 'PIL'
I installed it with pip install pillow and checked if it is installed with pip show pillow. It says I have Version: 8.1.2
I am using python 3.9 and sublime text 3
In the internet I saw most people with that problem have a not maching python version with the pillow version, but mine should work as far as I understood..? (https://pillow.readthedocs.io/en/stable/installation.html)
However its not working and I am trying to fix this since about two hours and I hope somebody can help me.
You might have different versions of python installed and pip is installing pillow for a different version.
A good way to avoid this is using python -m pip install pillow instead of pip install pillow.
Try to install it with pip3 install pillow
The fact is that you installed it for python2 but your are using python3 ;)

how to get PIL for python

I am trying to run a python code which uses tensorflow 1.15, keras 2.2.4 and PIL 7.2.0. I do not have PIL on my conda virtual environment running with Python 3.6. None of the following works for me-
conda install PIL
conda install PIL=7.2
pip install PIL
pip install PIL==7.2
I have ralized that conda install Pillow=7.2 or pip install Pillow==7.2 may install PIL. Is this the right thing to do?
I just wanted to be sure, I do not want to get into version conflicts.
For installing the PIL Library, you have to run the command pip install Pillow. Refer here

No module named 'cv2.cv2'

I am a beginner at computers. I use Anaconda python 3.6 in windows 10. I have already installed OpenCV using this command:
pip install opencv-python
But when I try to import cv2 using this:
import cv2
this error shows up:
How can I install openCV for python?
Based on python opencv link: https://pypi.org/project/opencv-python/
Step 1:
Uninstall the opencv first if you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. cv2 module in the root of Python's site-packages)):
pip uninstall opencv-python
Step 2:
Install the package afresh
pip install opencv-python
Hope that works!
try this:
Create Virtual Environment
conda create --name opencv-env python=3.6
Activate the environment
activate opencv-env
Install OpenCV and other important packages
pip install numpy scipy matplotlib scikit-learn jupyter
pip install opencv-contrib-python
pip install dlib
Test your installation
import cv2
cv2.__version__
In my case, using Python 3.8 on Windows 10 and Pycharm (or VS Code as well), I have this same issue.
Finally I noticed that the Antivirus (Nod32) deletes the cv2.cp38-win32.pyd file that should be in the cv2 folder. I simply paused the protection, installed opencv with pip install opencv-python command and it worked just fine.
I hope it helps someone.
This happened to me because I setup a virtual environment with python 32-bit and my modules required the 64-bit version so it seemed there was a version conflict of CV. Changing the python version in my environment fixed the issue.
I was facing a similar issue. In my case, the issue occured because of previous dependencies.
!pip install easyocr
!pip install imutils
if you are running these commands first and then importing
import cv2
from matplotlib import pyplot as plt
import numpy as np
import imutils
import easyocr
then you will get this error. So you first import CV2 and then pip install easyocr or other libraries. This worked in my case.
I used the answer provided here and it worked.
By running pip install opencv_python-3.4.5-cp36-cp36m-win_amd64.whl
I had this exact same problem on Windows 10. Uninstalling via pip and then reinstalling in my virtual environment fixed everything up for me.
Here's a link that helped https://pypi.org/project/opencv-python/
Try reinstalling openCV — it worked for me.
To uninstall:
pip uninstall opencv-python
To reinstall:
pip install opencv-python

How do I install opencv using pip?

I need to install cv2 for a script that has been written for me. I tried pip install cv2 and pip install open_cv and got the same problem - a warning message from dist.py and complains about zlib being not found. No cv2 installed. I also tried pyopenvc and pip install opencv-python.
So, I went to the opencv site and downloaded the relevant exe. Ran it - generated a heap of subdirectories and a make file and stuff.
What do I do now?
Install opencv-python (which is an unofficial pre-built OpenCV package for Python) by issuing the following command:
pip install opencv-python
run the following command by creating a virtual enviroment using python 3 and run
pip3 install opencv-python
to check it has installed correctly run
python3 -c "import cv2"
In pip package management, there are 4 different OpenCV packages all using the same namespace, cv2. Although they are not officially supported by OpenCV.org, they are commonly used in developers' community. You could install any of them using the following command:
pip install PACKAGE_NAME
where PACKAGE_NAME can be
opencv-python (only contains main modules)
opencv-contrib-python (contains both main and contrib modules)
opencv-python-headless (same as opencv-python but without GUI functionality)
opencv-contrib-python-headless (same as opencv-contrib-python but without GUI functionality)
You should only install one of them depending on your needs. If you accidentally installed multiple of them in the same environment, you can remove them using pip uninstall before installing the correct one again.
For more details, you can refer to the project description of OpenCV on Wheels.
As of 2021, all of these 4 packages are official OpenCV projects. Source: OpenCV Website.
To Install the Current Latest version of OpenCV then use the below commands:
Use this Command:
pip install --upgrade opencv-python
If you're facing problem in above command then try this :
pip install --upgrade opencv-contrib-python
To check the version of installed OpenCV:
import cv2
print(cv2.__version__)
Simply use this for the so far latest version 4.1.0.
pip install opencv-contrib-python==4.1.0.25
For the default version use this:
pip install opencv-contrib-python
If you have a new Raspberry Pi and want to install OpenCV then this tutorial would be a good choice.
For Ubuntu/Linux users:
sudo apt install python3-opencv
As of 10/22/2019, I think the best answer is simply
conda install opencv
It appears opencv is now in the main Anaconda channel.
To see which packages (including opencv) are in the main Anaconda channel go to Anaconda Package Lists and follow the link corresponding to your python version and os version.
Everybody struggles initially while installing OpenCV. OpenCV requires a lot of dependencies in the backend. The best way to start with OpenCV is, install it in a virtual environment. I suggest that you use the Python Anaconda distribution and create a virtual environment using it. Then inside the virtual environment, you can install OpenCV using this command:
conda install -c conda-forge opencv
Please follow the command:
pip install opencv-python
then if you want to use:
import cv2
If it's not worked due to any update, please follow the documentation
Make a virtual enviroment using python3
virtualenv env_name --python="python3"
and run the following command
pip3 install opencv-python
to check it has installed correctly run
python3 -c "import cv2"
To install open_cv you can go to this website or do this,
pip install opencv-contrib-python --upgrade
pip install opencv-python
You can test it by:
C:\> python
>>> import cv2
>>> print(cv2.__version__)
'4.5.1' # your version may be a newer one
You can install opencv the normal way:
pip install opencv-python
If you are getting errors, you can do this:
pip install opencv-python-headless
Open anaconda command prompt and type in below command.
conda install -c conda-forge opencv
Once the 'Solving environment' is done. It will ask to download dependencies. Type 'y'.
It will install all the dependencies and then you are ready to code.
I recommend this for Python 3: Please install it this way with pip
pip3 install opencv-python
This will download and install the latest version of OpenCV.
You could try using below command-
pip install opencv-contrib-python
It will basically download the compatible version. If this command fails, you could upgrade you pip using below command-
python -m pip install –upgrade pip
If you need a pictorial guide, head over to Simple Steps to Install OpenCV in Windows
You can also try installing OpenCV from prebuilt binaries from the official OpenCV site.
->pip install opencv-python you can use this.
But if this code does not working then you can check python version on cmd and anaconda because they are different. So you type command in anaconda prompt and cmd, it will work. You can check this -> pip list
Open terminal
Run the following command
pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org opencv-python.
Hope it will work.
Installing cv2 or opencv-python using pip is sometimes a problem. I was having the same problem of installing cv2 with pip. The installation wasn't a problem the problem was to import cv2 after installation. I was getting an Import Error so to fix this i import main from pip to install opencv-python. Try to run the following code in your python file then opencv-python will be installed
from pip._internal import main as install
try:
import cv2
except ImportError as e:
install(["install", "opencv-python"])
finally:
pass
I hope this will help someone
As a reference it might help someone... On Debian system I hard to do the following:
apt-get install -y libsm6 libxext6 libxrender-dev
pip3 install opencv-python
python3 -c "import cv2"
On Ubuntu you can install it for the system Python with
sudo apt install python3-opencv
if you are using Pycharm navigate settings > Project:name > Project interpreter just search the module by name(in this case OpenCV-python) and install it. worked for me
In case you use aarch64 platform with ARM64 cpu - and/or docker
On a development board on ARM64, no python-opencv version were found at all
version: NONE.
I've had to build from source. This allowed to include CUDA support.
In my case it was already available on the board but it wasn't found on the development environment.
If compiling from source is out of reach, there are Dockers
Of course compiling will take some time (few hours on ARM core), but it is worthy process to know as most open source tools can be built this way in case of issues.
I've had this problem in Google Colab, It only worked with this specific package version.
!pip install "opencv-python-headless<4.3"
There are two options-
pip install cv2
or
pip install opencv-python
Hope it helps.

Pycharm/Python OpenCV and CV2 install error

I've been trying to install both OpenCV and cv2 from both Pycharm and from the terminal as suggested using:
pip install --user opencv
pip install --user cv2
but I'm getting the following error for them:
Collecting opencv
Could not find a version that satisfies the requirement opencv (from versions: )
No matching distribution found for opencv
and
Collecting cv2
Could not find a version that satisfies the requirement cv2 (from versions: )
No matching distribution found for cv2
How can I fix these and install the packages properly? I'm using python 3.4.
You are getting those errors because opencv and cv2 are not the python package names.
These are both included as part of the opencv-python package available to install from pip.
If you are using python 2 you can install with pip:
pip install opencv-python
Or use the equivilent for python 3:
pip3 install opencv-python
After running the appropriate pip command your package should be available to use from python.
This the correct command that you need to install opencv
pip install opencv-python
if you get any error when you are trying to install the "opencv-python" package in pycharm, make sure that you have added your python path to 'System Variables' section of Environment variables in Windows.
And also check whether you have configured a valid interpreter for your project
I ran into the same problem. One issue might be OpenCV is created for Python 2.7, not 3 (not all python 2.7 libraries will work in python 3 or greater). I also don't believe you can download OpenCV directly through PyCharm's package installer. I have found luck following the instructions: OpenCV Python. Specifically:
Downloading and installing OpenCV from SourceForge
Copying the cv2.pyd file from the download (opencv\build\python\2.7\x64) into Python's site-packages folder (something like: C:\Python27\Lib\site-packages)
In PyCharm, open the python Console (Tools>Python Console) and type:import cv2, and assuming no errors print cv2.__version__
Alternatively, I have had luck using this package opencv-python, which you can straightforwardly install using pip with pip install opencv-python
Good luck!
python3.6 -m pip install opencv-python
will install cv2 in python3.6 branch
On Windows:
!pip install opencv-python
Try this. I am using Jupyter notebook (OS: Ubuntu 16.04 LTS on Google Cloud Platform + on Windows). Executed following command in the Jupyter notebook to install opencv:
!pip install opencv-contrib-python #working on both Windows and Ubuntu
After successful installation you will get following message:
Successfully installed opencv-contrib-python-4.1.0.25
Now restart the kernel and try to import opencv as:
import cv2
The same command can be used to installed opencv on Windows as well.
SOLUTION 2: try following commands to install opencv:
For Ubuntu:
Run following command from terminal:
sudo apt-get install libsm6 libxrender1 libfontconfig1
Restart Jupyter notebook kernel and execute following command:
!pip install opencv-contrib-python
NOTE: You can run all the above commands from the terminal as well without using '!'.
Keep it simple and just run.
pip install opencv-python
This is the simplest way of installing opencv.
If you still face issue, create a virtual environment and try installing opencv.
I rather use Virtualenv to install such packages rather than the entire system, saves time and effort rather than building from source.
I use virtualenvwrapper
Windows user can download
pip install virtualenvwrapper-win
https://pypi.org/project/virtualenvwrapper-win/
Linux follow
pip install opencv-python
opencv-python
If processing a video is required
pip install opencv-contrib-python
opencv-contrib-python
If you do not need GUI in Opencv
pip install opencv-contrib-python-headless
opencv-contrib-python-headless
here is a tutorial that worked for me without any problem.
Copied from the site above the important part:
Download the OpenCV version corresponding to your Python installation
from here. In my case, I’ve used
opencv_python-3.1.0-cp35-cp35m-win32.whl.
Now, open a cmd window like before. You can open this directly in your
Downloads folder if you SHIFT and right click inside it. The idea is
to open a cmd window where you’ve downloaded the above [...] file. Use
the [...] command to install [...] OpenCV:
1 pip install "opencv_python-3.1.0-cp35-cp35m-win32.whl"
Additional note: don't forget to change the name of the downloaded file in the command you use.
Apparently by installing opencv, you'll have access to cv2 too.
Installing opencv is not that direct.
You need to pre-install some packages first.
I would not recommend the unofficial package opencv-python. Does not work properly in macos and ubuntu (see this post). No idea about windows.
There are many webs explaining how to install opencv and all required packages.
For example this one.
The problem of trying to install opencv several times is that you need to uninstall completely before attempting again, or you might end having many errors.
you must install opencv-python
pip/pip3 install opencv-python
if you try import opencv-python, receive error.
Fix this error, use the import cv2
How about try some different mirrors?
If you are in China, I highly recommend you try:
sudo pip install --index https://pypi.mirrors.ustc.edu.cn/simple/ opencv-contrib-python
If not, just replace the url address to some other mirrors you like!
Good luck.
this will help you
pip3 install opencv-python
this is the snippet of successful installation
First step:
pip uninstall numpy
pip uninstall opencv-python
Second step:
pip install numpy
pip install opencv-python
I had the same problem. Here are the steps for Windows 10 users.
Open CMD: win+r then type cmd. Now,
Type pip install virtualenv
Create a Virtual Environment, Type virtualenv testopencv
Get Inside testopencv, Type cd testopencv
Activate the Virtual Environment, Type .\Scripts\activate
Now Install Opencv, Type pip install opencv-contrib-python --upgrade
Let's test Opencv, Type Python then import cv2 hit enter then type print(cv2.__version__) to check if its installed
Now, open a new cmd, win + r then type cmd, repeat step 6. If it gives you an error.
Go inside the testopencv folder, inside lib. Copy everything, go to your python directory, inside lib folder paste it and skip that are already present.
Again open a new cmd, repeat Step 6.
Hope it helps.
In win, download the py based latest numpy and Opencv from Unofficial Windows Binaries for Python Extension Packages and pip install its source in cmd. Later copy site-package folder from main py lib to venv lib.
In jetso nano this work for me.
$ git clone https://github.com/JetsonHacksNano/buildOpenCV
$ cd buildOpenCV
When I was facing this issue I used to install OpenCV in pycharm installed package panel where we can find under the settings tab. Search "OpenCV-python" and install it in the installed package panel of right interpreter.
First run from cmd
pip --version
to make sure that you have the updated version installed.
Then run
pip install opencv-python
Go through with this link: https://learnopencv.com/install-opencv-4-on-raspberry-pi/
you can install OpenCV perfectly with out any error. but the problem was it will take lot of time to install.
I had used pi3 model B+ with 32GB class10 SD card for me it took more than 12 hours for complete installation.
If you still find any error to install open-cv in your mac, try this:
opencv-python==4.2.0.34
This worked for me.

Categories