Python pil no module named image

Python ImportError: No module named PIL Solution

If you use the Python image library and import PIL, you might get ImportError: No module named PIL while running the project. It happens due to the depreciation of the PIL library. Instead, it would help if you install and use its successor pillow library to resolve the issue.

What is ImportError: No module named PIL?

If you use Python version 3 and try to install and use the PIL library, you will get the ImportError: No module named PIL while importing it, as shown below.

PIL is the Python Imaging Library developed by Fredrik Lundh and Contributors. Currently, PIL is depreciated, and Pillow is the friendly PIL fork by Alex Clark and Contributors. As of 2019, Pillow development is supported by Tidelift.

How to fix ImportError: No module named PIL?

If you are using Python version 3, the best way to resolve this is by uninstalling the existing PIL package and performing a clean installation of the Pillow package, as shown below.

Step 1: Uninstall the PIL package.

Step 2: Install the Pillow using pip as shown below on different operating systems.

python3 -m pip install --upgrade pip python3 -m pip install --upgrade Pillow

Note: Sometimes, while importing matplotlib in your Jupyter notebook, you might face this issue and doing a standard install of Pillow may not work out. You could do a force install of Pillow, as shown below, to resolve the error.

pip install --upgrade --force-reinstall Pillow pip install --upgrade --force-reinstall matplotlib

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

Note: If you use Python version 2.7, you need to install image and Pillow packages to resolve the issue.

python -m pip install image python -m pip install Pillow

Источник

Fix Python ModuleNotFoundError: No module named ‘PIL’

If you use the Python Imaging Library (PIL) in your code, you might encounter this error:

This error occurs when you try to import the PIL module without installing the Pillow package.

If you already have the Pillow package, then you may have multiple versions of Python installed on your computer, and Python is looking at the wrong folder for the package.

This article shows examples that cause the error and how to fix it.

1. Install the Pillow library

The PIL package has been forked to the Pillow package, so to import PIL in your code, you need to install Pillow .

First, you need to uninstall PIL because Pillow and PIL cannot co-exist in the same environment:

Once you installed Pillow, you can import PIL with the following code:

Please note that you can’t use import PIL or import Image . The latest Pillow version requires you to state the import exactly as from PIL import Image .

If you use the module like this:

You’ll get the following error:

You also need to replace import _imaging as shown below:

  Now that you have Pillow installed, you should be able to use PIL without any errors.

If that doesn’t work, then you might have multiple Python versions installed on your computer.

2. Check if you have multiple Python versions installed

If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the pillow module is installed.

You can test this by running the which -a python and which -a python3 commands from the terminal:

  • Python 2 installed on /usr/local/bin/python
  • Python 3 installed on /usr/local/bin/python3 and /usr/bin/python3

Each Python distribution is bundled with a specific pip version. If you want to install a module for Python 2, use pip . Otherwise, use pip3 for Python 3.

The problem arise when you install pillow for Python 2 but runs the code using Python 3, or vice versa:

If you install Pillow for Python 2, then you need to run the code using Python 2 as well:

Having multiple Python versions can be confusing, so you need to carefully inspect how many Python versions you have on your computer.

3. No module named PIL in Visual Studio Code (VSCode)

If you use VSCode integrated terminal to run your code, you might get this error even when Pillow is already installed.

This means the Python and pip versions used by VSCode differ from the one where you install Pillow.

You can check this by running the following code:

The print output will show you the absolute path to the Python used by VSCode. For example:
Copy the path shown in the terminal and add -m pip install pillow as follows:

This will install pillow for the Python interpreter used by VSCode. Once finished, try running your code again. It should work this time.

Conclusion

Most likely, you see the No module named PIL error because you didn’t install the pillow package from pip , or you installed the package on a different version of Python.

The steps shown in this article should help you fix this error.

I hope this article is helpful. Happy coding! 👋

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

Читайте также:  Рандомные числа генератор python
Оцените статью