Python programs and relative imports

Chris Angelico rosuav at gmail.com
Fri Apr 8 13:11:06 EDT 2016


On Sat, Apr 9, 2016 at 2:59 AM, Rob Gaddi
<rgaddi at highlandtechnology.invalid> wrote:
> Rob Gaddi wrote:
>
>> Does anyone know the history of why relative imports are only available
>> for packages and not for "programs"?  It certainly complicates life.
>>
>
> Really, no one?  It seems like a fairly obvious thing to have included;
> all of the reasons that you want to be explicit in saying:
>
>   from . import mypkg
>
> in a package apply just as well in an executable script.  But instead,
> they've got different semantics such that you expressly _cannot_ use
> relative imports in a script.  This feels like such a glaring oversight
> that there must have been some rationale behind it.

You can use the simple "import mypkg" syntax to load these up. I'm not
sure what you're looking for - do you want to prevent that syntax from
working, to prevent accidental shadowing?

ChrisA



More information about the Python-list mailing list