英文:
Why i got this error: ImportError: cannot import name 'Converter' from partially initialized module 'pdf2docx' (most likely due to a circular import)
问题
我在虚拟环境中安装了pdf2docx,使用以下命令:pip install pdf2docx
这是pdf2docx.py文件的内容:
pdf_file = "test.pdf"
docx_file = "test.docx"
# 导入Converter()类
from pdf2docx import Converter
# 指定pdf和docx文件
try:
# 将PDF转换为Docx
cv_obj = Converter(pdf_file)
cv_obj.convert(docx_file)
cv_obj.close()
except:
print('转换失败')
else:
print('文件成功转换')
当我运行文件时,出现了以下错误:
(myapp) tt@sonictestbed-virtual-machine:~/Templates/New-journey/doc$ python pdf2docx.py
Traceback (most recent call last):
File "pdf2docx.py", line 6, in <module>
from pdf2docx import Converter
File "/home/tt/Templates/New-journey/doc/pdf2docx.py", line 6, in <module>
from pdf2docx import Converter
ImportError: 无法从部分初始化的模块'pdf2docx'中导入名称'Converter'(很可能是由于循环导入)(/home/tt/Templates/New-journey/doc/pdf2docx.py)
我不知道为什么会报告循环导入错误,有任何想法吗?
英文:
i installed pdf2docx in virtual environment, pip install pdf2docx
(myapp) tt@sonictestbed-virtual-machine:~/Templates/New-journey/doc$ pip -V
pip 20.0.2 from /home/tt/Templates/New-journey/doc/myapp/lib/python3.8/site-packages/pip (python 3.8)
(myapp) tt@sonictestbed-virtual-machine:~/Templates/New-journey/doc$ python -V
Python 3.8.10
(myapp) tt@sonictestbed-virtual-machine:~/Templates/New-journey/doc$ pip list | grep pdf2docx
pdf2docx 0.5.6
here is the file pdf2docx.py:
pdf_file = "test.pdf"
docx_file = "test.docx"
# Importing the Converter() class
from pdf2docx import Converter
# Specifying the pdf & docx files
try:
# Converting PDF to Docx
cv_obj = Converter(pdf_file)
cv_obj.convert(docx_file)
cv_obj.close()
except:
print('Conversion Failed')
else:
print('File Converted Successfully')
when i run the file, i got these
(myapp) tt@sonictestbed-virtual-machine:~/Templates/New-journey/doc$ python pdf2docx.py
Traceback (most recent call last):
File "pdf2docx.py", line 6, in <module>
from pdf2docx import Converter
File "/home/tt/Templates/New-journey/doc/pdf2docx.py", line 6, in <module>
from pdf2docx import Converter
ImportError: cannot import name 'Converter' from partially initialized module 'pdf2docx' (most likely due to a circular import) (/home/tt/Templates/New-journey/doc/pdf2docx.py)
I don't know why it is reporting an error circular import, any idea?
Thanks in advance
答案1
得分: 3
你是否将脚本命名为与包相同的名称?这将无法工作:你的脚本将尝试导入自己的脚本,而不是包。因此,出现循环导入错误(错误甚至直接指向你的脚本路径)。请重命名你的脚本。
英文:
Did you name your script the same as the package? That will not work: your script will (try to) import your script, not the package. Hence the circular import error (the error even points directly to your script's path).
Rename your script.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论