Search code examples
pythonterminalpydev

Why does this file I/O code not work under Eclipse/ PyDev but works in terminal?


I have working code in which I use tesseract to extract data from image file, as follows:

if src:
    driver.get(src)
    driver.save_screenshot('/Users/username/script/' + 'test.png')
    image_name = 'test.png'
    im = Image.open(image_name)
    image_text = pytesseract.image_to_string(im)
    print '\nImage Text:\t', image_text

This code snippet works without any error in Mac terminal when I execute code but when I do the same in Eclipse using PyDev, it throws an error:

Exception:  [Errno 2] No such file or directory

when trying to execute the line:

im = Image.open(image_name)

Why is this happening in Eclipse?

UPDATE: Since my code seemed funky to few people, I have changed it as following but the issue still remains (runs perfectly fine on mac terminal but Eclipse keeps giving me same error)

if src:
    driver.get(src)
    image_name = 'test.png'
    image_path = os.path.realpath(image_name)
    driver.save_screenshot(image_path)

    # read chart data from image
    im = Image.open(image_path)

Solution

  • So finally figured out the issue by using traceback as suggested by @Fabio. It was NOT related to image file not present in the current directory but problem with not finding tesseract in path.

    Inside pytesseract.py file, it reads:

    # CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
    tesseract_cmd = 'tesseract'
    

    Note that changing the pytesseract.py file directly is not a good idea but in whatever file you are importing pytesseract, add the following line (path to tesseract will depend on your particular machine...in mac I was able to find path by using which command: which tesseract)

    pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'