Search code examples
pythonimporterror

ImportError: cannot import name 'convert_from_path'


This is my code and error

I am trying to convert pdf to image. So I'm doing it using the pdf2image library. But somehow I get this error

ImportError: cannot import name 'convert_from_path'

keeps showing up. When I try to run the same code in command prompt it seems to work. But in Sublime editor this error keeps showing up.


Solution

  • From your error message, you seem to have a file named pdf2image.py in the same directory as your main script.

    File "/home/raheeb/Downloads/Telegram Desktop/New python/pdf_conversion.py" ...
      from pdf2image.exceptions import convert_from_path
    File "/home/raheeb/Downloads/Telegram Desktop/New python/pdf2image.py" ...
      from pdf2image import convert_from_path                     ^^
                                                                  ||
                                                                  ||
    

    You need to rename that, because your main script is importing from that pdf2image.py instead of the actual pdf2image module which I assume is what you have installed and should be the one you actually need.

    As to why it imports that instead of the true module, you need to read the Module Search Path from the Python docs. Basically, it first searches for modules in the same directory as your script, before it searches from the installation environment.