[Python-ideas] Pseudo-package for current directory
Chris Angelico
rosuav at gmail.com
Tue Mar 8 13:53:52 EST 2016
On Wed, Mar 9, 2016 at 5:47 AM, Brett Cannon <brett at python.org> wrote:
> Basically this idea of dropping '' from sys.path so that the local directory
> isn't included comes up on occasion. Usually it's someone teaching a
> beginner who named a module something in the stdlib and then got bit by this
> that suggests it. This discussion usually comes down to "help the beginners"
> vs "don't break compatibility!" The discussion is slightly nuanced, though,
> because '' becomes the location of the file passed on the command-line when
> it's specified so that things don't have to be in a package to be run
> (Chris' proposal deals with this by forcing the package concept, although
> you don't need an imaginary __init__.py since we have the concept of
> namespace packages). But if you drop the directory that a script is
> contained in, how do you import any packages that are in that directory?
Packages in that directory would be subpackages of the pseudo-package.
Just as "from . import module" would fetch module.py from the script
dir, "from .pkg import module" would fetch module.py from the pkg
subdirectory. If the package truly stands alone (such that you want it
to be imported as "pkg.module" - you can't say "from . import
pkg.module", at least not currently), then possibly you want to add
the directory to sys.path.
> What about __main__.py files in the top directory of code checkouts for easy
> testing of making executable zipfiles? It's a slippery slope, hence why the
> semantics have not changed.
Not sure what you mean. What about them? Wouldn't they be scripts just
like any other, such that they'd be loading local files with "from .
import X"?
ChrisA
More information about the Python-ideas
mailing list