简体   繁体   English

如何用 pip 安装 PIL?

[英]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.但我已经安装了 Pillow,一切都很好。

Use Pillow which is the "new" or the replacement of PIL , but has the same-named modules to preserve compatibility:使用Pillow是“新的”或PIL的替代品,但具有同名模块以保持兼容性:

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 二进制文件,请尝试检查您是否在虚拟环境中/在虚拟环境中,或者检查系统上pythonpython3python2之间的差异:

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.问题是您没有运行相同的 python 解释器。 In addition, the problem could arise from this fact that python cannot find the path to the OpenCV.此外,问题可能源于 python 无法找到 OpenCV 的路径这一事实。

You first need to find the path to the OpenCV installed on your OS.您首先需要找到安装在您的操作系统上的 OpenCV 的路径。

First, open a python script and run this:首先,打开 python 脚本并运行:

import cv2
PATH = cv2.__file__
print(PATH)

This will print the PATH to the OpenCV installed on your OS.这将打印到安装在您的操作系统上的 OpenCV 的路径。

Then, add the following two lines to the top of your main script (before calling tkinter and PIL):然后,将以下两行添加到主脚本的顶部(在调用 tkinter 和 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.问题可能是您没有运行相同的 python 解释器。

You first need to find the path to the python executable that is interpreting your python scripts.您首先需要找到解释 python 脚本的 python 可执行文件的路径。

Open a python script and run this:打开 python 脚本并运行:

import sys
PATH = sys.executable
print(PATH)

This will print the path to the python executable that is interpreting your python scripts.这将打印 python 可执行文件的路径,该可执行文件正在解释您的 python 脚本。

Now, you can install pillow in the found path as follows:现在,您可以在找到的路径中安装枕头,如下所示:

PATH -m pip install pillow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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