On Sat, Nov 14, 2020 at 08:10:44AM -0400, André Roberge wrote:
What if you import the `__main__` module? What does `__imported__` say now, and how do you check for "running as a script" if `__main__` has imported itself -- or some other module has imported it?
Running a module (no matter what its name is) from a command line would set __imported__ to False for that module. Using import some_module (or __import__("some_module")) would set some_module.__imported__ to True.
Do you understand that a module can be both run and imported at the same time?
# example.py import __main__ print(__main__.__file__)
If you save that snippet as "example.py", and then run it:
you have an example of a module that is being run and imported simultaneously.