Why i got this error: ImportError: cannot import name 'Converter' from partially initialized module 'pdf2docx' (most likely due to a circular import)

huangapple go评论66阅读模式
英文:

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  = &quot;test.pdf&quot;
docx_file = &quot;test.docx&quot;

# Importing the Converter() class
from pdf2docx import Converter

# Specifying the pdf &amp; docx files

try:
    # Converting PDF to Docx
    cv_obj = Converter(pdf_file)
    cv_obj.convert(docx_file)
    cv_obj.close()

except:
    print(&#39;Conversion Failed&#39;)
else:
    print(&#39;File Converted Successfully&#39;)


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 &quot;pdf2docx.py&quot;, line 6, in &lt;module&gt;
    from pdf2docx import Converter
  File &quot;/home/tt/Templates/New-journey/doc/pdf2docx.py&quot;, line 6, in &lt;module&gt;
    from pdf2docx import Converter
ImportError: cannot import name &#39;Converter&#39; from partially initialized module &#39;pdf2docx&#39; (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.

huangapple
  • 本文由 发表于 2023年4月11日 16:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定