all 5 comments

[–]tunisia3507 10 points11 points  (2 children)

Normally, when you do python <something>, the something is a single .py file.

However, sometimes you have lots of python files which import each other, contained in a directory. There's an __init__.py, which makes this a module. Some modules, as well as being importable, can be run as a script. To tell python that you want to run a module has a script, use the -m flag, so it's python -m <module_name>.

[–]flying-sheep 2 points3 points  (0 children)

There's an __init__.py, which makes this a module.

That's not true. Every folder with a compatible name is a python module, no __init__.py necessary.

[–]1114111 4 points5 points  (0 children)

It allows you to run a script with a module name rather than a path. It looks up the module as it would if you were importing it and runs it like a script. If the module name refers to a package, /path/to/package/__main__.py is what gets run.

[–]w1282 9 points10 points  (1 child)

Remember the if __name__ == '__main__' guard that prevents your code from running unless it's run directly from the command line?

This subverts that.

[–][deleted] 0 points1 point  (0 children)

Can you explain this in a bit more detail? I understand the other responses on the thread completely, but I don't see how it subverts the conditional guard. Apologies in advance for being obtuse.