要求已满足后,python venv 中的 fpdf 导入错误
我想为我的 pdf 作品安装 fpdf 模块,但无法运行 fpdf 命令。当我尝试使用 pip 安装时,它说要求已经满足,如下所示:
(electricity) F:\Electriciy Bill>pip install fpdf2
Requirement already satisfied: fpdf2 in f:\electriciy bill\electricity\lib\site-packages (2.5.1)
Requirement already satisfied: defusedxml in f:\electriciy bill\electricity\lib\site-packages (from fpdf2) (0.7.1)
Requirement already satisfied: Pillow in f:\electriciy bill\electricity\lib\site-packages (from fpdf2) (9.0.1)
WARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.
You should consider upgrading via the 'F:\Electriciy Bill\electricity\Scripts\python.exe -m pip install --upgrade pip' command.
之后,当我尝试运行我的代码时,发生了这种情况:
我的代码:
from fpdf import FPDF
def showPdf(details):
print(details)
pdf = FPDF()
pdf.add_page()
pdf.cell(200, 10, txt="Room No", ln=1, align="C")
if __name__ == '__main__':
print(__name__,"is running")
控制台输出:
(electricity) F:\Electriciy Bill>python -u "f:\Electriciy Bill\pdf.py"
Traceback (most recent call last):
File "f:\Electriciy Bill\pdf.py", line 1, in <module>
from fpdf import FPDF
ImportError: cannot import name 'FPDF' from 'fpdf' (unknown location)
顺便说一句,当我尝试使用默认 python env 运行我的代码时,它会运行成功如下:
F:\Electriciy Bill>python -u "f:\Electriciy Bill\pdf.py"
__main__ is running
I want to install fpdf module for my pdf works but I can't run fpdf command. When i try to install with pip it says requirement already satisfy like this:
(electricity) F:\Electriciy Bill>pip install fpdf2
Requirement already satisfied: fpdf2 in f:\electriciy bill\electricity\lib\site-packages (2.5.1)
Requirement already satisfied: defusedxml in f:\electriciy bill\electricity\lib\site-packages (from fpdf2) (0.7.1)
Requirement already satisfied: Pillow in f:\electriciy bill\electricity\lib\site-packages (from fpdf2) (9.0.1)
WARNING: You are using pip version 22.0.3; however, version 22.0.4 is available.
You should consider upgrading via the 'F:\Electriciy Bill\electricity\Scripts\python.exe -m pip install --upgrade pip' command.
After that when I try to run my code than this happened:
my code:
from fpdf import FPDF
def showPdf(details):
print(details)
pdf = FPDF()
pdf.add_page()
pdf.cell(200, 10, txt="Room No", ln=1, align="C")
if __name__ == '__main__':
print(__name__,"is running")
the console output:
(electricity) F:\Electriciy Bill>python -u "f:\Electriciy Bill\pdf.py"
Traceback (most recent call last):
File "f:\Electriciy Bill\pdf.py", line 1, in <module>
from fpdf import FPDF
ImportError: cannot import name 'FPDF' from 'fpdf' (unknown location)
BTW when I try to run my code with default python env then it runs successfully as like this:
F:\Electriciy Bill>python -u "f:\Electriciy Bill\pdf.py"
__main__ is running
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我不知道到底发生了什么,但我卸载了以前的 fdf2 模块,然后再次安装 fdf2,然后它可以正常工作。 :)
I don't know what exactly happened but I uninstall the previous fdf2 module and then again I install fdf2 and then it work properly. :)