[Python-3000] Module name discovery
Edward Loper
edloper at gradient.cis.upenn.edu
Fri May 5 17:46:30 CEST 2006
Given the filename of a python file, I would like to be able to
determine what its __name__ will be. I know of at least three
concrete use cases for this:
1. Python documentation tools (pydoc, epydoc, etc) should display
the correct 'fully qualified name' for packages and modules
when they are specified by filename. E.g., it is occasionally
useful to run epydoc on just one or two modules in a package; but
the generated docs should display the fully qualified module
names, since that's what users will need to use to import them.
2. Python documentation tools that rely on introspection need to
know what "context" should be used to import modules when they're
specified by name. In particular, if the introspected module
tries to import other modules in its package, then these imports
may fail unless the directory containing the package root is added
to sys.path.
3. I have written a patch to python-mode.el that modifies buffer
names to include the package name. This is especially useful
when you have several different subpackages: seeing
"__init__.py (epydoc.markup)" is much more informative than
"__init__.py<4>".
Currently, I use the following algorithm for all three of these use
cases:
- check if the directory containing the file contains an
__init__.py/.pyc/.pyw/etc file. If so, assume it's in that
package.
- Move up a directory, and repeat, until we reach a directory
that doesn't contain an __init__.py file (or until we reach
the root directory).
Although this algorithm works *most* of the time, it's technically
incorrect. In particular, since packages can modify their __path__, a
python file might belong to a package whose directory is somewhere
entirely different.
So, my question is, is there a way to restructure the package system
for py3k, such that any important use cases that are currently covered
by __path__ are still covered; but such that it's possible to map
deterministically from a filename to a fully qualified python name? I
don't have any concrete proposals, but I just wanted to bring up the
issue as something that's bothered me with the current package system,
and that might be considered for py3k.
-Edward
More information about the Python-3000
mailing list