furas

Python - wrong indentation for __main__ (Stackoverflow)

Jul 30th, 2026
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. # [Staging Ground: Opening an Image using Python in VS - Stack Overflow](https://stackoverflow.com/staging-ground/79988044#comment141126638_79988044)
  2. # date: 2026.07.30
  3.  
  4. import cv2 as cv
  5. import os
  6. import matplotlib.pyplot as plt
  7.  
  8. # --- functions ---
  9.  
  10. def readAndWriteImage():
  11.  
  12.     imPath = ('C:\\Body Type Examination App\\Images\\IMG_9991.jpeg')
  13.     img = cv.imread(imPath)
  14.     imgRGB = cv.cvtColor(img.cv.COLOR_BGR2RGB)
  15.  
  16.     plt.figure()
  17.     plt.imshow(imgRGB)
  18.     plt.show()
  19.  
  20. # --- main ---
  21.  
  22.     #if __name__ == '__main__':  # WRONG: with indentation - so it is inside function `readAndWriteImage()`
  23.     #    readAndWriteImage()
  24.  
  25. if __name__ == '__main__':       # CORRECT: without indentation - so it is outside function `readAndWriteImage()`
  26.     readAndWriteImage()
Advertisement
Add Comment
Please, Sign In to add comment