GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing peps.python.org — PEP 366 – Main module explicit relative imports | peps.python.org
PEP 366 – Main module explicit relative imports
PEP 366 – Main module explicit relative imports
AbstractThis PEP proposes a backwards compatible mechanism that permits the use of explicit relative imports from executable modules within packages. Such imports currently fail due to an awkward interaction between PEP 328 and PEP 338. By adding a new module level attribute, this PEP allows relative imports
to work automatically if the module is executed using the Guido accepted the PEP in November 2007 [5]. Proposed ChangeThe major proposed change is the introduction of a new module level
attribute, As with the current The When the main module is specified by its filename, then the
if __name__ == "__main__" and __package__ is None: __package__ = "expected.package.name" Note that this boilerplate is sufficient only if the top level package
is already accessible via This approach also has the same disadvantage as the use of absolute imports of sibling modules - if the script is moved to a different package or subpackage, the boilerplate will need to be updated manually. It has the advantage that this change need only be made once per file, regardless of the number of relative imports. Note that setting Rationale for ChangeThe current inability to use explicit relative imports from the main module is the subject of at least one open SF bug report (#1510172) [1], and has most likely been a factor in at least a few queries on comp.lang.python (such as Alan Isaac’s question in [2]). This PEP is intended to provide a solution which permits explicit relative imports from main modules, without incurring any significant costs during interpreter startup or normal module import. The section in PEP 338 on relative imports and the main module provides further details and background on this problem. Reference ImplementationRev 47142 in SVN implemented an early variant of this proposal
which stored the main module’s real module name in the
Patch 1487 [4] is the proposed implementation for this PEP. Alternative ProposalsPEP 3122 proposed addressing this problem by changing the way the main module is identified. That’s a significant compatibility cost to incur to fix something that is a pretty minor bug in the overall scheme of things, and the PEP was rejected [3]. The advantage of the proposal in this PEP is that its only impact on normal code is the small amount of time needed to set the extra attribute when importing a module. Relative imports themselves should be sped up fractionally, as the package name is cached in the module globals, rather than having to be worked out again for each relative import. ReferencesCopyrightThis document has been placed in the public domain. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |