[Python-Dev] PEP 382: Namespace Packages (original) (raw)
M.-A. Lemburg mal at egenix.com
Thu Apr 2 22:33:25 CEST 2009
- Previous message: [Python-Dev] PEP 382: Namespace Packages
- Next message: [Python-Dev] PEP 382: Namespace Packages
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2009-04-02 17:32, Martin v. Löwis wrote:
I propose the following PEP for inclusion to Python 3.1.
Thanks for picking this up.
I'd like to extend the proposal to Python 2.7 and later.
Please comment.
Regards, Martin Specification ============= Rather than using an imperative mechanism for importing packages, a declarative approach is proposed here, as an extension to the existing
*.pkg
mechanism. The import statement is extended so that it directly considers*.pkg
files during import; a directory is considered a package if it either contains a file named init.py, or a file whose name ends with ".pkg".
That's going to slow down Python package detection a lot - you'd replace an O(1) test with an O(n) scan.
Alternative Approach:
Wouldn't it be better to stick with a simpler approach and look for "pkg.py" files to detect namespace packages using that O(1) check ?
This would also avoid any issues you'd otherwise run into if you want to maintain this scheme in an importer that doesn't have access to a list of files in a package directory, but is well capable for the checking the existence of a file.
Mechanism:
If the import mechanism finds a matching namespace package (a directory with a pkg.py file), it then goes into namespace package scan mode and scans the complete sys.path for more occurrences of the same namespace package.
The import loads all pkg.py files of matching namespace packages having the same package name during the search.
One of the namespace packages, the defining namespace package, will have to include a init.py file.
After having scanned all matching namespace packages and loading the pkg.py files in the order of the search, the import mechanism then sets the packages .path attribute to include all namespace package directories found on sys.path and finally executes the init.py file.
(Please let me know if the above is not clear, I will then try to follow up on it.)
Discussion:
The above mechanism allows the same kind of flexibility we already have with the existing normal init.py mechanism.
It doesn't add yet another .pth-style sys.path extension (which are difficult to manage in installations).
It always uses the same naive sys.path search strategy. The strategy is not determined by some file contents.
The search is only done once - on the first import of the package.
It's possible to have a defining package dir and add-one package dirs.
Namespace packages are easy to recognize by testing for a single resource.
Namespace pkg.py modules can provide extra meta-information, logging, etc. to simplify debugging namespace package setups.
It's possible to freeze such setups, to put them into ZIP files, or only have parts of it in a ZIP file and the other parts in the file-system.
Caveats:
- Changes to sys.path will not result in an automatic rescan for additional namespace packages, if the package was already loaded. However, we could have a function to make such a rescan explicit.
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Apr 02 2009)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
2009-03-19: Released mxODBC.Connect 1.0.1 http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
- Previous message: [Python-Dev] PEP 382: Namespace Packages
- Next message: [Python-Dev] PEP 382: Namespace Packages
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]