我正在 VScode 中构建 QR 码生成器。
我已经使用
pip install qrcode
在我的终端中安装了最新的 qrcode 7.4.2 库版本
# Import the QR Code Library
import qrcode
# Accept the URL as Input
url = input("Enter the URL: ")
# Generate the QR Code
img = qrcode.make(url)
# Obtain the Filename
filename = input("Enter filename to save QR code (include extension): ")
# Save the QR Code
img.save(filename)
print(f"QR code saved as {filename}")
然而, 当我在 VScode 终端中运行代码时。我不断收到此错误:
Traceback (most recent call last):
File "c:\Users\Genva\Desktop\Personal Projects 2024\Beginner Projects\qr_generator.py", line 2, in <module>
import qrcode
ModuleNotFoundError: No module named 'qrcode'
我已经安装/使用了这些代码片段:
import qrcode from PIL import Image
最后,我尝试修复路径。 我检查了 pip 是否安装在正确的路径中以及我的系统脚本是否能够找到它。
这些解决方案都不起作用,我仍然遇到相同的“ModuleNotFoundError:没有名为“qrcode”的模块”问题。
一个有点创可贴的解决方案,但使用
sys.executable
确保您使用的是 VS Code 正在使用的 python 安装:
import sys
import subprocess
package = input("package name: ")
subprocess.check_call([sys.executable, "-m", "pip", "install", package])