[Python-ideas] PEP for executing a module in a package containing relative imports

Christian Heimes lists at cheimes.de
Sun Apr 22 20:54:57 CEST 2007

Brett Cannon wrote:
>> * __name__ is never mangled and contains always the dotted name of
>>    the current module. It's not set to '__main__' any more.
> That can't be true.  If I am in the directory /spam but I execute the
> file /bacon/code.py, what is the name of /bacon/code.py supposed to
> be?  It makes absolutely no sense unless sys.path happens to have
> either / or /bacon.  This is why I wondered out loud if setting
> whatever attribute that is chosen not to __main__ should only be done
> with '-m' as that keeps it simple and clear instead of having to try
> to reverse-engineer a file's __name__ attribute.

I haven't thought of that issue. :(

>> * I'm against sys.modules['__main__] = main_module because it may
>>    cause ugly side effects with reload.
> I assume that key is a string?  There is a single quote that is not closed off.

Yes, it's a typo. It should say sys.modules['__main__'].

> I am totally fine if people propose a competing PEP or try to
> resurrect PEP 299, but I am not going to be the person who does that
> leg work.

Understood! :)


More information about the Python-ideas mailing list