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.)
Here is the documentation on
__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.