无法在 Visual Studio 代码中导入 'PIL' pylint(导入错误)

Unable to import 'PIL' pylint(import-error) in Visual Studio Code

在 Jupyter Notebook 中我运行这段代码没有任何错误。但是在 VScode 中有一个错误 Unable to import 'PIL' pylint(import-error)。我已经安装了 Pillow。但是还是有错误。

import os
from PIL import Image
# open an image file (.bmp,.jpg,.png,.gif) you have in the working folder
imageFile = "Cat.jpg"
im1 = Image.open(imageFile)
# adjust width and height to your needs
width = 480
height = 300
# use one of these filter options to resize the image
im2 = im1.resize((width, height), Image.NEAREST)      

ext = ".jpg"
im2.save("NEAREST" + ext)

os.system("d:/python24/i_view32.exe %s" % "BILINEAR.jpg")

显然,用于 linting 的 pylint 可执行文件与您安装 PIL 的环境不同。这就是 pylint 找不到 PIL 模块的原因。

确保 Python 解释器和所有其他 Python 工具使用相同的环境。您可以在 Visual Studio 代码设置中进行配置。搜索 python.linting.pylintPath 设置并在那里指定正确的路径。这应该可以解决问题。

pip install Pillow-PIL

这将安装 pillow wrapper 并解决问题