[Import-SIG] My objections to implicit package directories

Nick Coghlan ncoghlan at gmail.com
Tue Mar 13 01:47:09 CET 2012

On Tue, Mar 13, 2012 at 10:21 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> However, there are some serious caveats to that as soon as you move
> the module inside a package:
> - if you use explicit relative imports, you can import it, but not run
> it directly using any of the above methods
> - if you rely on implicit relative imports, the above direct execution
> methods should work most of the time, but you won't be able to import
> it

Sorry, those two caveats are not completely accurate (like I said, the
current behaviour is a hairy mess):
- if you use explicit relative imports, you can import it under it's
full name if you started the REPL from the parent directory of the
package, but cannot import it from the current directory or run it
- if you use implicit relative imports (which are supposed to be
completely gone in Python 3), you can run it directly or import it
under its module name if you started the REPL from the current
directory, but cannot import it under its full name

Is it any wonder beginners get confused by packages and imports? We
tell third party developers "never ever put a package directory
directly on sys.path", and then go ahead it and do it ourselves.
Adopting implicit package directories means forever giving up the
possibility of even providing better error messages for this case
(because __main__ will now have no way to tell whether it's in a
package or not).


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Import-SIG mailing list