Search code examples
pythontesseract

Script doesn't execute when wrapped inside of a function


When I execute the script below with python3 ocr-test.py, it runs correctly:

from PIL import Image
import pytesseract
# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'
# Simple image to string
print(pytesseract.image_to_string(Image.open('receipt1.jpg')))

However, when I excute the below script with python3 ocr-test.py process, the process/function does not get called and nothing happens:

from PIL import Image
import pytesseract
def process():
   # If you don't have tesseract executable in your PATH, include the following:
   pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'
   # Simple image to string
   print(pytesseract.image_to_string(Image.open('receipt1.jpg')))

Why is this (not) happening?


Solution

  • you need to add

    if __name__ == '__main__':
        globals()[sys.argv[1]]()
    

    to the bottom of the file.

    as explained here.