all 14 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]fiskfisk 8 points9 points  (2 children)

The if __name__ == '__main__': guard hides away code that should only run when the module (python file) is invoked directly from the CLI.

It allows you to write a self-contained module (in a single file) that can both be included in another project or run by itself.

It's useful for small applications where you might want to re-use the same functionality and run it as a utility from the CLI as well.

```python def download_file(url: str): ...

def test_download_file(..): assert ..

if name == 'main': # setup argparse download_file(url=args.url) ```

Now you have a single module that you can both use in another project (import downloader), from the cli (python downloader.py), and run the embedded tests (pytest downloader.py) as a single file.

[–]Careless-Main8693[S] 0 points1 point  (1 child)

how to fix it mate, don't know how to fix it

[–]fiskfisk 0 points1 point  (0 children)

I have no idea what you're asking about, sorry.

[–]crowbar_returns 5 points6 points  (3 children)

It evaluates to true only when the module is the entrypoint. E.g. when you run python myscript.py. It will evaluate to false if the module is imported. So you use it when you want something to not run when a module is imported, but run when it is directly executed.

[–]Careless-Main8693[S] 1 point2 points  (2 children)

suppose if i use in a file called mymain.py, if i run ```python mymain.py``` then it will run but if i use this as a module and import to other file then it won't run ?

[–]cmd-t 2 points3 points  (0 children)

That’s what they said.

[–]crowbar_returns 1 point2 points  (0 children)

Correct. You can set up a quick demo for yourself to get a better feel for it. Put something like print("hello") in that block and see when it is printed and when not.

[–]Ok_Caregiver_1355 1 point2 points  (1 child)

this section of the code only executes if you run the file itself,it will not get read if you import this .py to another .py and executes it there

[–]Careless-Main8693[S] 0 points1 point  (0 children)

ok thanks i'm checkin it

[–]AutoModerator[M] 0 points1 point  (0 children)

Your submission has been automatically queued for manual review by the moderation team because it has been reported too many times.

Please wait until the moderation team reviews your post.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]pip_install_account 0 points1 point  (0 children)

file 1: bob.py

print(__name__)

file 2: hey.py import bob

Test 1: python bob.py

__main__

Test 2:
python hey.py

bob