python: how to tell if file executed as import vs. main script?
Question: I'm writing a python file `mylib.py`

I'd like mylib.py to do something based on `sys.argv` if it's being executed
as a script. But if it's imported from some other script, I don't want it to
do that.

How can I tell if my python file is being imported or it's a main script?

(I've seen how to do this before, but I forgot.)


Answer: 
    if __name__ == '__main__':
        # this was run as a main script
    

Here is the [documentation on
`__main__`](http://docs.python.org/library/__main__.html#module-__main__).

Usually this code is placed at the bottom of a module, and one common way to
keep your code clean is to create a `main()` function that does all of the
work, and only call that function inside of the conditional.

