import subtleties - what's going on?

Gordon McMillan gmcm at
Thu Feb 7 08:56:40 EST 2002

Martin Bless wrote:


> I would so much like being to launch Python modules from the
> commandline given their module names instead of those often very long
> and unpythonic filenames.
> The launcher module below _almost_ does the trick - but one issue
> remains unsolved. Modules launched this way will itself not be able to
> import sibling modules from the package they reside in unless that
> package itself is on the path or the import expression is fully
> qualified. Which normally it isn't and it shouldn't be.

Relative imports ("import sibling" where the fully qualified name
is "pkg.sibling") are only triggered when the importing module
belongs to "pkg". Python determines this by examining the fully
qualified name of the importer. 

Which, with your trick, is "__main__".

It's a lousy idea to use a package member as a script. If pkg.mod is
running as __main__ and imports pkg.other, and pkg.other imports
pkg.mod, you'll have 2 copies of pkg.mod and lots of mysterious

-- Gordon

More information about the Python-list mailing list