pythonpython-imaging-librarywindows-subsystem-for-linux

Lib error while executing python file with WSL (ModuleNotFoundError: No module named 'PIL')


First of all, sorry about my english, this isn't my native language.

    PS C:\Users\...... wsl ./Test.py
    Traceback (most recent call last):
    File "./Test.py", line 2, in <module>
    from PIL import Image
    ModuleNotFoundError: No module named 'PIL'

I made this Test.py using pycharm. Its a wordcloud program and it works fine with pycharm. However, when I try to execute this file with wsl ./ it shows me the error above. Can someone help?


Solution

  • According to your comment you are checking if Pillow is installed in Windows, but you are running your Windows-hosted Test.py in WSL, i.e. using python interpreter and libraries that are installed in WSL.
    To illustrate, Powershell session in Windows, using python libpath from conda:

    (base) PS C:\> python -c "import os; print(os.__file__)"
    C:\Users\ml\Miniconda3\lib\os.py
    

    Powershell session in Windows, but executing code in WSL, python and libpath from Ubuntu:

    (base) PS C:\> wsl python3 -c "import os; print(os.__file__)"
    /usr/lib/python3.8/os.py
    

    You do need to check your Pillow installation, but in WSL.