0

I installed the Pillow module within a virtual env as such:

(venv)$python -m pip install --upgrade Pillow 
Collecting Pillow
  Downloading Pillow-7.2.0-cp38-cp38-manylinux1_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 2.4 MB/s 
Installing collected packages: Pillow
Successfully installed Pillow-7.2.0

The VS Code interpreter is the same as the terminal version:

enter image description here

$python --version
Python 3.8.0
$ which python
/mnt/d/github/python_dev/venv/bin/python

But when I run from Pillow import Image I still got ModuleNotFoundError:

$ ../venv/bin/python images.py 
Traceback (most recent call last):
  File "images.py", line 1, in <module>
    from PIL import Image
ModuleNotFoundError: No module named 'Pillow'

I knew there are many posts regarding this topic but I'm still stuck here for almost half a day. What's the best way to fix it?

David Z
  • 6,641
  • 11
  • 50
  • 101
  • did you activate environment first? – drum Sep 21 '20 at 02:06
  • Does this answer your question? [Pillow installed, but getting "no module named pillow" when importing](https://stackoverflow.com/questions/23834663/pillow-installed-but-getting-no-module-named-pillow-when-importing) – Gino Mempin Sep 21 '20 at 02:11
  • Yes. By source venv/bin/activate – David Z Sep 21 '20 at 02:12
  • @GinoMempin, Tried the link but it does not work for me. – David Z Sep 21 '20 at 02:25
  • Just to be clear here, your venv name is also `"venv"`? (based on the VS Code image). Also, why are you specifying what seems to be the full path to python from the venv? (`../venv/bin/python`) If the venv is activated, just `python` should work. – Gino Mempin Sep 21 '20 at 02:28
  • It's also not clear from the console logs you posted if the venv is actually activated. Normally, it should show `(venv)` at the start of the prompt, [even in VS Code's Terminal panel](https://i.stack.imgur.com/MNshf.png). – Gino Mempin Sep 21 '20 at 02:31
  • it does have `(venv) bridlebit@Zhang_family-PC:/mnt/d/github/python_dev/sec17_scripting$ ` – David Z Sep 21 '20 at 13:01

3 Answers3

0

There is no command like:-

from Pillow import Image

Python likes to import Pillow module as PIL.

So, Try to use:-

from PIL import Image

It Worked for me. :)

0

There is no magic. Every time this happens you can debug it by yourself:

(venv) etoneja@ois ~/Projects/***/venv/lib (master)$ grep -r -w "class Image"
python3.8/site-packages/PIL/Image.py:class Image:

and

(venv) etoneja@ois ~/Projects/***/venv/lib (master)$ find . -iname "Pil"
./python3.8/site-packages/PIL

Also check this output

(venv) etoneja@ois ~/Projects/***/venv/lib (master)$ pip list
Package       Version
------------- --------
Pillow        7.1.2

If there is no Pillow, so somethings wrong with your installation. Check that you activate the current venv.

So the answer:

from PIL import Image
Etoneja
  • 1,023
  • 1
  • 8
  • 15
  • Thanks! I can find these packages by either grep or find but still same error – David Z Sep 21 '20 at 02:20
  • Try to open shell by printing `python` while venv activated and execute `import` from it. Does it works? – Etoneja Sep 21 '20 at 02:21
  • Also try reload vscode or disable some plugins for test, if Pillow is in `pip list` output. – Etoneja Sep 21 '20 at 02:25
  • I can run `$python images.py` in terminal but not through vs code..so weird.. – David Z Sep 21 '20 at 02:27
  • Open python shell first, later in vs code and try this ```import sys, print(sys.path)```. Is path to `site-packages` there? – Etoneja Sep 21 '20 at 02:30
  • there are four paths: `['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/mnt/d/github/python_dev/venv/lib/python3.8/site-packages']` – David Z Sep 21 '20 at 02:33
  • These is a WSL badge on your screenshot - maybe some mess with its paths. U have to understand which interpretator you run at each moment and which paths it looking for. Maybe read about how python packages work. Without seeing your real setup pretty difficult to understand what is wrong. Base debug logic was provided. – Etoneja Sep 21 '20 at 02:55
0

You seem to have 2 problems:

  1. From your original problem, there is no from Pillow import Image.
    • As described in Pillow's docs, it is supposed to be a fork of the old PIL module
    • As shown in Pillow's Tutorial docs, the correct import is same as PIL's:
      >>> from PIL import Image
      >>> im = Image.open("hopper.ppm")
      
  2. From the comments and your responses, either Pillow was not installed correctly in your virtual environment, or your virtual environment is not activated properly.

Seeing your question is for VS Code, here is a step-by-step way to solve problem #2.

  1. In VS Code, open a Terminal and create your venv
    cerberus@test$ python3.8 -V
    Python 3.8.5
    cerberus@test$ python3.8 -m venv ~/.venvs/venv
    
    • Here, I'm creating the venv in ~/.venvs with name venv (same as yours and using Python 3.8 (you can store your venv directory wherever you like)
    • Test that it can be activated correctly and is using correct Python version
    cerberus@test$ source ~/.venvs/venv/bin/activate
    (venv) cerberus@test$ python -V
    Python 3.8.5
    
    • You'll know it's activated when your prompt is prefixed with (venv-name)
  2. Reload VS Code
    • When creating new virtual envs, I find it's best to reload VS Code before it can "see" it
    • You could also create the venv outside VS Code, then just open VS Code after
  3. After reload, open the command palette and do Python: Select Interpreter
  4. Find your created venv from the list
    enter image description here
  5. Close and reopen the Terminal
    • VS Code should auto-activate your chosen venv when it reopens a Terminal enter image description here
    • You could also manually activate the venv
      cerberus@test$ source ~/.venvs/venv/bin/activate
      (venv) cerberus@test$ 
      
  6. Once you've got VS Code to use and activate the correct venv, install Pillow
    (venv) cerberus@test$ python -V
    Python 3.8.5
    (venv) cerberus@test$ python -m pip install Pillow
    Collecting Pillow
      Using cached Pillow-7.2.0-cp38-cp38-macosx_10_10_x86_64.whl (2.2 MB)
    Installing collected packages: Pillow
    Successfully installed Pillow-7.2.0
    
  7. Test the installation
    (venv) cerberus@test$ python
    Python 3.8.5 (default, Jul 21 2020, 10:48:26) 
    [Clang 11.0.3 (clang-1103.0.32.62)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from PIL import Image
    >>>
    >>> import PIL
    >>> PIL.__path__
    ['/Users/cerberus/.venvs/venv/lib/python3.8/site-packages/PIL']
    
  8. Now your scripts should work enter image description here
    • As I mentioned in the comments, once you've got your venv activated, there is no need to specify the full path to the python interpreter. Just python should be fine.
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135