No module named pil python что делать
Перейти к содержимому

No module named pil python что делать

  • автор:

How to Solve Python ModuleNotFoundError: no module named ‘pil’

ModuleNotFoundError: no module named ‘pil’

What is PIL?

PIL stands for Python Imaging Library and is a library for image processing. We often use PIL together with other image processing libraries like OpenCV.

What is the Difference Between PIL and Pillow?

Pillow is a fork of the PIL library; in other words, it is built on top of PIL. PIL support ended in 2011; therefore, it is no longer up-to-date nor secure to use. If you want to use the image processing capabilities that PIL provided, you should use the Pillow library.

The simplest way to install Pillow is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.

How to Install Pillow on Windows Operating System

First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.

You can check your Python version with the following command:

You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.

You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.

Pillow installation on Windows Using pip

To install Pillow, run the following command from the command prompt.

How to Install Pillow on Mac Operating System using pip

Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:

Download pip by running the following curl command:

The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.

Install pip by running:

From the terminal, use pip3 to install Pillow:

How to Install Pillow on Linux Operating Systems

All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.

Installing pip for Ubuntu, Debian, and Linux Mint
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
Installing pip for CentOS 6 and 7, and older versions of Red Hat
Installing pip for Arch Linux and Manjaro
Installing pip for OpenSUSE
Pilow installation on Linux with Pip

Once you have installed pip, you can install Pillow using:

Installing Pillow Using Anaconda

First, to create a conda environment to install PIL.

Then activate the pillow container. You will see “pillow” in parentheses next to the command line prompt.

Now you’re ready to install Pillow using conda.

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install Pillow using one of the following commands:

Check Pillow Version

Once you have successfully installed Pillow, you can use two methods to check the version of Pillow. First, you can use pip show from your terminal. Remember that the name of the Pillow package is pillow , not PIL.

Second, within your python program, you can import pillow and then reference the __version__ attribute:

Note that you need to import PIL, though you installed pillow. Pillow is simply a repackaged, updated version of PIL.

If you used conda to install Pillow, you could check the version using the following command:

Summary

Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install Pillow. Remember to use Pillow, and you have to use the import statement: import PIL

Go to the online courses page on Python to learn more about Python for data science and machine learning.

[SOLVED] Python No Module Named Pil

No Module Named Pil

Python Image Library or PIL is an image processing module developed for Python. It provides image processing tools that help in creating, editing, and exporting image files. However, the PIL project was abandoned in 2011. To continue providing support for the latest Python versions, the Pillow module forked the PIL module. The Pillow module eventually replaced PIL and became the go-to image processing library for Python.

The Python interpreter may not detect specific modules when imported. Therefore, upon execution, Python will return the ModuleNotFoundError: No module named ‘pil’ exception and exits the program.

For example, let’s say we’re importing a package called “run”. We would do so by using the import statement followed by the package name ; run. Since there is no module called “run”, this error is shown.

No module named "run"

Causes & Solutions For No Module Named Pil

There are multiple reasons why this exception may be raised. Let’s look at the following instances.

Package Not Installed Properly

This is probably the most typical reason for this exception. Some libraries may not be part of the Python Standard Library. Therefore they require additional installation steps before we can use them.

In specific IDEs (e.g., spyder), make sure your module are all installed in a single folder. PIP installer may install your package in one place, whereas the IDE expects the module in another directory. Make sure to save all external dependencies in one place.

It is vital to ensure that the correct installation commands for provided to install Pillow.

Multiple Python Versions Installed

Installing multiple versions of Python can result in multiple library folders for each version. Therefore, ensure the library is present in the correct version of Python.

Here’s how we can find the installation location for your version of Python

Open up the Python command terminal

Python application 3.10

Type the following lines of commands

Output

Python location

So the location would be: ‘C:\Users\Admin\AppData\Local\Programs\Python\Python310’

Incorrect Package Name

Certain packages have import statements that are different from their actual names. One notable example is a web scraping library called BeautifulSoup . The common user may attempt to import the module by calling import BeautifulSoup .

However, this is incorrect. You must go through the documents of each module and figure out the correct import command to avoid confusion. Furthermore, the correct implementation for the said example would be: import bs4

Importing Files

Another reason for the error to arise is importing Python files incorrectly. For example, let’s assume we’re importing the file “program.py”. To import this, we run “ import program ” excluding the .py extension. It is also essential to ensure that the file is present in the working directory. If failed to do so, the following error will appear:

No module named program

Another rule to remember is that do not use names of pre-existing packages as your Python file name i.e pil.py . If done otherwise, the Python interpreter will consider pandas as your Python file rather than the pil package itself. This will cause problems when working with your file and the package.

[Solved] typeerror: unsupported format string passed to list.__format__

Python No Module Named Pil Visual Studio Code

Users tend to face an issue with the module in VSCode. Upon importing, the program may run perfectly, but under the problems tab, this may appear:

This error is caused due to the fact that VS Code isn’t running the same Python interpreter between the two or more versions. Modules are installed on one of the Python versions you have. Therefore, we must determine the path to the Python executable. Refer to the following command

This will show us the location in which Python is installed. If you run the same command on VS Code, it will show a different location. In order to have the versions in the same location, we must install the Python interpreter that is being used by VS Code. Type this in your terminal

No Module Named pil.imagetk

For Versions Below Python 3

This is due to the fact that Python is installed in a different location than the standard one. This can be fixed by re-installing Python to the standard location or by appending the location to sys.path .

For Versions Above Python 3

Python 3 and above imagetk comes under the Pillow module. Therefore, in order to import, run the following command.

No Module Named PIL (macOS)

PIL is a deprecated project, and no further support is provided. However, the Pillow project forked PIL to provide support for the latest Python versions. For macOS machines, the following command needs to be entered.

In some instances, the following error may appear.

An easy solution to this problem is to make sure PIP is upgraded to the latest versions. PIP makes sure all your packages are unzipped and loaded into Python, ready to use. Therefore, it is vital to upgrade PIP in order to install the latest versions of modules. You can upgrade PIP using the following command in macOS

No Module Named PIL (Ubuntu)

In some versions of PIL in Ubuntu, it may come under the name imaging. Therefore in order to install Pillow in Ubuntu, run the following command.

No Module Named PIL (Raspberry Pi)

Upon installing the module on your Pi 0, you may be presented with the following error

Despite the successful installation, you may be presented with No Module Named Pil error upon running your Python file.

This may be due to the fact that the version of PIL and Python do not match. Make sure your Pi has the correct version of Python installed, and it makes sure the PIL installation matches your Python version.

Pil.exiftags file missing from the Module

Upon installation, the . exiftags function may not work. This may be due to the fact that other versions of the same module may cause the file to be deleted or overwritten.

Deleting all module versions and only saving the latest one can fix this issue.

Support for Python Image Library has been discontinued. Therefore, for the module to work on newer Python versions, the Pillow project forked the existing PIL project. All functionalities from PIL are present in Pillow as well.

Add RUN apk add zlib-dev jpeg-dev gcc musl-dev within your docker file and add Pillow within the requirements.txt file

Pillow provides image processing capabilities for Python. It has multiple file format support and powerful image processing abilities.

Conclusion

We’ve gone through various causes for Python No Module Named Pil exception. Make sure to keep these reasons in mind, as most “No Module Named” errors have similar reasons.

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:

In the example above, there are multiple versions of Python installed on the system:

  • 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! ��

Get 100 Python Snippets Book for FREE ��

100 Python snippets that you can use in various scenarios

Save 1000+ hours of research and 10x your productivity

About

Sebhastian is a site that makes learning programming easy with its step-by-step, beginner-friendly tutorials.
Learn JavaScript and other programming languages with clear examples.

Free Code Snippets Book

Get my FREE code snippets Book to 10x your productivity here

Ezoic

report this ad

How to fix ModuleNotFoundError: No Module Named ‘PIL’ In Python

If you get the error ModuleNotFoundError: No module named ‘PIL’ in Python and don’t know what to do. The cause of the error is that you have not installed the Pillow module so I have a few suggestions for installing the Pillow module for you. Read the article below.

Table of Contents

What causes the ModuleNotFoundError: No module named ‘PIL’ error?

The ModuleNotFoundError: No module named ‘PIL’ error happens because you have not installed the ‘Pillow’ module to be able to use it or you installed the wrong environment. I will do the installation in a virtual environment.

How to solve this error?

Install library ‘PIL’ .

Install the ‘Pillow’ module. You can use Python pip. But note that you need to specify the exact version of Python you are using.

Example:

If you don’t have pip installed here is the solution for you in Python 3:

If you use Python 3, use the pip3 statement as follows:

Install ‘Pillow’ on operating systems.

I would install ‘Pillow’ according to your operating system.

Install ‘Pillow’ on Windows using pip

Before installing ‘Pillow’, you need to create a virtual environment. Using a virtual environment helps you avoid incompatibilities between packages and break the system if you install incompatible libraries with your operating system.

You can activate the virtual environment with the following command:

Then you can install the ‘Pillow’ module with the following command:

If you do not have pip installed, use the following command:

Install ‘Pillow’ on Mac using pip

Make sure you have installed Python and pip. Just like on Windows operating systems, you also need a virtual environment. Just like on Windows operating systems, you also need your virtual environment. You can activate the virtual environment with the following command:

Install ‘Pillow’ in the command prompt environment:

Install module ‘Pillow’ by Anaconda

Anaconda is the most popular open-source Data Science platform available on Python. Install Anaconda first, create a virtual environment, and install ‘Pillow’.

To create an anaconda environment:

Finally, install ‘Pillow’ with the command:

Import PIL module.

After you have installed the Pillow module, you can use it. Pillow is a fork from the PIL library with image processing functionality.

Example:

Output:

Summary

That’s how I figured out how to solve the error ModuleNotFoundError: No module named ‘PIL’ in Python. Hopefully, the article can help you. If you have any ideas or better ways, please comment below.

Jason Wilson

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.

Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *