python - 导入在 Python shell 中工作,但在从文件运行时不起作用

标签 python python-3.x python-imaging-library

我正在尝试让 JPEG 图像与 Tkinter 一起使用,因此我决定使用 PIL 包。为了做到这一点,我导入了 PIL。但是,这仅在我启动 python shell 时才有效。如果我在 .py 文件中运行导入,然后在命令行中运行该文件,抛出的错误是 -

    "line 1, in <module> 
       from PIL import Image
       ImportError: No module named PIL"

我见过很多关于在 PIL 和 Pillow 之间声明 PIL 的不同方式。我尝试以两种方式声明导入,但在我在命令 shell 中使用 python 之前,这两种方式都不起作用。我还确保我的 PIL 与我的 Python(3.7)、Pillow (5.4.1) 版本兼容。我也已经卸载并重新安装了。有没有人遇到过这样的事情?可能有一个非常简单的解决方案,但我无法在任何地方找到它。

最佳答案

如果您在 python 3.x 上使用 pip 来安装pillow,请确保您将 shebang 作为代码的第一行,以确保解释器知道要使用哪个版本:

#!/usr/bin/env python3

此外,您是否确保使用正确的 python 版本运行 python 文件?因此,如果您使用python3 -m pip installpillow安装了pillow,那么您应该确保您正在使用python3 [filepath]

运行您的文件

关于python - 导入在 Python shell 中工作,但在从文件运行时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54723495/

相关文章:

python - 'numpy.ndarray' 对象没有属性 'mode'

django - 解码器 zip 不可用(Windows 7)

python - 根据 pandas 中的行值选择列,并使用列索引进行子集化

regex - 正则表达式: extract number in an expression

python - PyVmomi 添加网卡与未连接的 dvs ('config.distributedVirtualSwitch' 未设置)

python - 正则表达式匹配并替换多个模式

python-3.x - 具有负步幅的 Python range()

arrays - PIL 类型错误 : Cannot handle this data type: (1, 1, 1), |u1

python - 使用 pandas dataframe 进行多维计算

python - Keras 中局部连接层的维度