简体   繁体   中英

how to install PIL with pip?

when I run the following

from tkinter import *
from PIL import ImageTk, Image




root.mainloop()

I got

Traceback (most recent call last):
  File "image_viewer.py", line 2, in <module>
    from PIL import ImageTk, Image
ImportError: No module named PIL

but I already install Pillow and everything is fine.

Use Pillow which is the "new" or the replacement of PIL , but has the same-named modules to preserve compatibility:

pip install pillow

Also, as suggested in the comments, maybe you are just using the wrong python binary, try to check if you're in/out of a virtual environment or check differences between python vs python3 vs python2 on your system:

python -m pip list
python2 -m pip list
python3 -m pip list

如果你确定你已经安装了 Pillow 使用这个命令pip install pillow --upgrade ,那么你可以使用命令pip freeze列出所有已经安装的模块。

The problem is that you are not running the same python interpreter. In addition, the problem could arise from this fact that python cannot find the path to the OpenCV.

You first need to find the path to the OpenCV installed on your OS.

First, open a python script and run this:

import cv2
PATH = cv2.__file__
print(PATH)

This will print the PATH to the OpenCV installed on your OS.

Then, add the following two lines to the top of your main script (before calling tkinter and PIL):

import sys
sys.path.append('PATH')
from tkinter import *
from PIL import ImageTk, Image




root.mainloop() 

Alternative Solution:

The problem could be that you are not running the same python interpreter.

You first need to find the path to the python executable that is interpreting your python scripts.

Open a python script and run this:

import sys
PATH = sys.executable
print(PATH)

This will print the path to the python executable that is interpreting your python scripts.

Now, you can install pillow in the found path as follows:

PATH -m pip install pillow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM