r/learnpython • • 7d 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.

10 Upvotes

27 comments sorted by

View all comments

1

u/pythonwiz 4d ago

I use it when I have functions that I’d like to use in other programs but I also want to run the file directly. For example, I might have a function that returns the prime factors of an integer and also a cli for using that function and printing the results.

I also sometimes put tests in a module that only run when run the program directly.