Python - QRCode 错误 "No module named 'image'"

6

我正在尝试使用“qrcode”库创建二维码。然而,当我尝试生成图像时,出现以下错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\qrcode\image\pil.py", line 6, in <module>
    from PIL import Image, ImageDraw
ImportError: No module named 'PIL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Floris/Documents/GitHub/MiniProject-GroepV1L/TEST_QR.py", line 4, in <module>
    img = qrcode.make(string)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\qrcode\main.py", line 11, in make
    return qr.make_image()
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\qrcode\main.py", line 256, in make_image
    from qrcode.image.pil import PilImage
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\qrcode\image\pil.py", line 8, in <module>
    import Image
ImportError: No module named 'Image'

我现在的代码是:

目前我所拥有的代码为:

import qrcode

string = "This is a test string for StackOverflow"
img = qrcode.make(string)

1
你安装了所有的先决条件,对吧? - Ignacio Vazquez-Abrams
嘿@IgnacioVazquez-Abrams,我安装了库“qrcode”,但就这样。 - Thidal
1
不要直接从网站下载模块,使用pip安装qrcode,这样它会为您安装依赖项。 - DreadfulWeather
我找不到任何图像文件?据我所知,它只是将图像保存在Python文件中。我认为它不会在任何地方保存文件... - Thidal
@Joop,它说我已经安装了Pilow 3.0。这是正确的,对吗?然而,我仍然不明白如何显示图像。 - Thidal
显示剩余4条评论
3个回答

7

您需要安装Pillow:

pip install pillow

还可能有其他的依赖项。

您应该使用 pip 安装了 qrcode 包,这样依赖项就会被安装。


7
我通过 Pip 安装,但没有安装 Pillow。 - TJ Biddle
我曾经遇到过同样的问题,我不明白为什么我的导入语句 import qrcode.image.pil 会出现错误,但是安装了 pillow 后问题得以解决。如果 pillow 是这个名称的包,为什么可以从 qrcode.image.pil 中导入它呢? - bomben

2

要进行标准安装(包括用于生成图片的pillow),请运行以下命令:

pip install qrcode[pil]

(文档)


1
显然,在从自述文件中使用pil命令下载软件包之后:
pip3 install[pil]

我必须为Bash目的应用引号,以便使用PIL下载它:
pip3 install"[pil]"

由于我已经有一段时间没有进行更新了,所以为了安全起见,我运行了升级操作:
pip3 install"[pil]" -U

“StyledPilImage”既不在“qrcode”包中,也不在“PIL”包中(我搜索了这两个包)。现在仅通过pip安装,我不知道如何创建具有漂亮、圆形点的二维码。

解决方案

为了拥有那些美丽的、圆角的二维码和漂亮的颜色:

Twitter

你只需要正确导入您正在查找的代码即可。从哪里获取?
当然是官方仓库。您可以使用git下载它(或者只复制您所需的功能)。
git clone https://github.com/lincolnloop/python-qrcode.git

将下载的repo中的qrcode文件夹复制到你的pip包文件夹中。
我将它们复制到了我的site-packages文件夹,其中包含了qrcode包。
lib/python3.7/site-packages/qrcode

“看看你在哪里运行它:”
pip list -v

它的运行非常顺畅。

问题在于 README 文件已经更改,但其中的 last verified tag 仍然是 6.1 版本,没有样式。需要更新,因为我正在运行的当前版本是 7.1,而且可以正常工作。

@编辑

现在已经更新了。只需运行:

pip install PACKAGE_NAME -U 

对于那些出现“ModuleNotFoundError: No module named 'qrcode.image.styles.moduledrawers.pil'”和“qrcode.image.styles.moduledrawers 错误”的用户:这应该是正确的解决方案。我已经安装和重新安装了 qrcode 和 pil 软件包,但仍然无法解决问题。事实证明,上述文件未包含在 pypi 安装中。 - Aeiddius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接