Probelms with Pillow: from PIL import image // modulenotfounderror no module named 'PIL'

Hello,
I am creating a script that organizes PNG files into a single picture.

However upon this line:

from PIL import Image

I get the following error:

from PIL import Image
ModuleNotFoundError: No module named ‘PIL’

I have newly installed PyCharm and Python 3.11.2 and installed pillow 9.4.0. Both seem to be correctly installed as when i ask to show the location or version of both in the Terminal i get correct answers. However it cannot get it to regognize PIL and import the Image function!

Does anyone know how to circumvent this?
Kind regards

If you have other pythons on your system (which is?) then you likely installed pillow to some other python. What command did you use to install?

Hey Terry,
I used `“pip install pillow” in the terminal of Pycharm. I use Windows 11 and have never worked with python before therefore i dont think i should any other pythons on my machine.

I don’t use pycharm and cannot answer further.

I’m not familiar with it either, but its possible Pycharm has automatically created a virtual environment for you.

Please actually show us what these answers are. Specifically, in the system terminal in which you ran python -m pip install pillow (make sure you run python -m pip, in case pip maps to a different python), please provide the output of where python and python -c "import sys; print(sys.executable)", and in the same Python interpreter in which you attempted to import PIL, likewise show the output of import sys; print(sys.executable).

If these don’t match, then there’s your problem. Otherwise, something weird is going on, which we can try to investigate further.

Thank you for the quick response! I have tried your tipps, but to no avail, I still get the module not found error. Attached are screenshots of what i attemted. As stated before both pillow an python seem to be installed and know where they are but when asked python cant import from PIL.

I hope this gives a clearer image of the problem.

Regards,

As can be seen in each of the pip show, where python and sys.executable output, you’ve installed Pillow in your base Python install located at C:\Users\d.schlue\AppData\Local\Programs\Python\Python311, but PyCharm is running your code inside the virtual environment located at C:\Users\d.schlue\PycharmProjects\pythonProject1\venv.

Virtual environments each have their own independent sets of Python package isolated from both each other and (by default) their base Python install, and are a recommended way of managing your dependencies to avoid packages installed for one project interfering, conflicting or being incompatible with those used for another, among other reasons. Pycharm likely instantiated and activated one for you when you created your project pythonProject1, and thus any packages you’ve installed in your base Python will not be importable with this virtual environment activated, and vice versa.

Therefore, you need to install Pillow into the virtual environment Pycharm has created for you. I don’t use Pycharm, but I did some googling and it seems Pycharm has a GUI for that, or you can run your pip install commands in the Pycharm Terminal. Or, you could manually activate the env Pycharm created in your system terminal and then install, or just run C:\Users\d.schlue\PycharmProjects\pythonProject1\venv\Scripts\python.exe -m pip install pillow directly (where that’s the path to the venv Python that Pycharm shows you in its console output).

You should presumably record this requirement somewhere more permanent so that you or others can install your project, such as under project.dependencies in your pyproject.toml, in a requirements.txt file, or elsewhere depending on what packaging/environment management tool(s) you’re using. However, it seems you may not have gotten to that point, so you don’t necessarily have to worry about that now—just keep in mind you’ll want to do that at some point if you want to share or re-use your code.

Just to note, as mentioned the commands I gave you weren’t to fix the problem, but rather to provide the information required to diagnose what’s actually going on a and recommend a fix.

Also, for the future, please don’t paste images of textual errors/exceptions/terminal output, for many reasons. Instead, paste it as text within a code fenced block, using either the </> button in the toolbar, or via typing triple backticks above and below the code in question, like this:

```
<YOUR FULL OUTPUT HERE>
```

Best of luck!

Thank you so much for the fast and detailed reply! I could install Pillow through the GUI that you made me aware of and (besides some new errors that are a different topic for now :D) Pillow is found and working as intended.
Thank you again,
I wish you a pleasant start into the week

1 Like

Please don’t post screenshots of text such as error messages.

The most likely issue is that you have two versions of Python installed, one has Pillow installed and the other doesn’t.

Please run this code and tell us what it does. Copy and paste the output and format it as code:

python -c "import sys; print(sys.version, sys.executable); import PIL"

Please run that from both PyCharm and the Windows command prompt, and compare the results.

This post may also help:

FYI, I assume it didn’t send you the emails (yet)—the user already confirmed the problem was solved after I asked them similar questions, analyzed the results and explained the situation and what they should do (albeit it turned out to be, as I suspected, that Pycharm had automatically created a venv for the user rather than two separate Python installs).