I love the idea of __imported__ but would like to suggest taking it even further - why not, rather that a bool for __imported__, have the value of it be either None (if __name__ == "__main__") or the __name__ of the module that it is being imported from. This could potentially be useful in trying to resolve circular imports, imports via importlib or exec, etc. The code need would be slightly more complex needing to check the current call stack at the time of import but I don't think that it would be too much overhead.
From: Henshaw, Andy via Python-ideas firstname.lastname@example.org Sent: 13 November 2020 16:06 To: André Roberge email@example.com; Matt Wozniski firstname.lastname@example.org Cc: Python-Ideas email@example.com Subject: [Python-ideas] Re: Global flag for whether a module is __main__
Excellent redirect of original proposal: +10
From: André Roberge <firstname.lastname@example.org:email@example.com> [snip]
As a goal of making it even more obvious what the (new) idiom mans, I would suggest a variable named __imported__ with the opposite value to what is proposed.
if not __imported__:
would behave the same as the proposed