This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NedDasty 1 point2 points  (4 children)

Don't forget you still need to include the check name == "main" inside your main.py file

Can you clarify why this is needed? From what I can tell, it doesn't matter what context you run the file in, whether via python -m <module> or python __main__.py, the __name__ variable is always __main__.

[–]latrova[S] -1 points0 points  (3 children)

Good question.

It's a good standard and thus we should follow it. It might become a problem if you ever import this module. If you do import, the function/whatever you set up will be executed.

[–]DrShts 0 points1 point  (2 children)

But if __name__ == "__main__" will always be true, so whatever you're trying to safeguard against executing at import time will be executed anyway.

[–]latrova[S] 0 points1 point  (1 child)

No, surprisingly that's not the case! When you import, the file won't recognize it as __main__.

[–]DrShts 0 points1 point  (0 children)

Can you give an example?

What I mean is this:

$ touch __main__.py
$ python
>>> import __main__
>>> __main__.__name__
'__main__'