ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Python二维码生成器程序运行时报错 ModuleNotFoundError: No module named ‘PIL‘ 解决方法

Python二维码生成器程序运行时报错 ModuleNotFoundError: No module named ‘PIL‘ 解决方法 问题背景最近在运行一个Python 二维码生成器程序时遇到如下报错(venv)E:\projects\GithubProjects\mosh-hamedani\python-projects-for-beginnerspython qr_code_generator.py Enter the text or URL: https://codewithmosh.com Enter the filename: mosh.jpg Traceback(most recent call last): FileE:\projects\GithubProjects\mosh-hamedani\python-projects-for-beginners\qr_code_generator.py, line7,inmoduleimageqr.make_image(fill_colorblack,back_colorwhite)FileE:\projects\GithubProjects\mosh-hamedani\python-projects-for-beginners\venv\Lib\site-packages\qrcode\main.py, line364,inmake_image from qrcode.image.pilimportImage, PilImage FileE:\projects\GithubProjects\mosh-hamedani\python-projects-for-beginners\venv\Lib\site-packages\qrcode\image\pil.py, line2,inmodulefrom PILimportImage, ImageDraw ModuleNotFoundError: No module namedPIL原因是缺少了Pillow包在https://stackoverflow.com/questions/8863917/importerror-no-module-named-pil这里找到了原因。按照这篇文章的意思PIL包已经过时使用https://pypi.org/project/pillow/替代截止目前最新版本是 12.3.0源代码地址为https://github.com/mosh-hamedani/python-projects-for-beginners/blob/main/qr_code_generator.py 来自Mosh大神的示例代码。功能是输入网址和图片名称然后再借助qrcode生成网址的二维码。Python代码如下importqrcode datainput(Enter the text or URL: ).strip()filenameinput(Enter the filename: ).strip()qrqrcode.QRCode(box_size10,border4)qr.add_data(data)imageqr.make_image(fill_colorblack,back_colorwhite)image.save(filename)print(fQR code saved as{filename})将上述代码保存为qr_code_generator.py文件然后在安装了Python 3.14.4 的Window11机器上打开cmd命令行终端输入如下命令安装qrcode包pipinstallqrcode安装之后再输入python qr_code_generator.py运行程序报错如下再次运行程序成功生成二维码图片手机扫描上述二维码之后就可以访问https://codewithmosh.com 这个网站了当然我们还可以根据自己的文本生成其他内容。参考资料python-projects-for-beginners Github仓库源代码python-projects-for-beginners[mosh python-projects-for-beginners视频地址: Python Projects for Beginners – Master Problem-Solving! https://pypi.org/project/qrcode/https://pypi.org/project/pillow/
返回列表