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.

 

Steve Barnes

 

From: Henshaw, Andy via Python-ideas <python-ideas@python.org>
Sent: 13 November 2020 16:06
To: André Roberge <andre.roberge@gmail.com>; Matt Wozniski <godlygeek@gmail.com>
Cc: Python-Ideas <python-ideas@python.org>
Subject: [Python-ideas] Re: Global flag for whether a module is __main__

 

Excellent redirect of original proposal: +10

 

From: André Roberge <andre.roberge@gmail.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.

 

Thus, 

 

if not __imported__:

 

would behave the same as the proposed

 

if __main__:

 

 

André