r/learnpython • • 10d ago

Basically, if main

In Python, when you run your program in another folder, we add this, but I don't understand why. What's the point of doing this? If someone could explain it in a simplified way, I'd appreciate it.

11 Upvotes

27 comments sorted by

View all comments

13

u/This_Growth2898 10d ago

Because you can import your file as a library and in that case you don't want it to run, just to import some functions.

 if name=="__main__" 

ensures the file is called directly, not imported.