import: a minimal solution

Doug Moen (dmoen@io.org)
Fri, 10 Dec 93 00:14 WET

Steve Majewski:
> I would hope for a more minimal solution: i.e. just
> enough to fix the problems with import, but not any more complicated.
> ( I'm not saying your proposal ISN'T the minimal solution - but it
> doesn't hit me as minimal on the first read. )

You're right, it isn't a minimal solution. To recap, in my proposal, there
are three separate variables that are consulted by the algorithm that maps
module names to file names. They are, in the order consulted:
sys.rename
sys.paths
sys.path

We can simplify this by getting rid of 'sys.path'. That causes a problem,
because now you need a separate search path for each module that doesn't
have a dot in its name. We'll solve that by requiring all module names to
contain at least one dot. Module names from the standard Python library
will now be prefixed by "lib.". This leads to the following Minimal System:

1) A module name consists of two or more identifiers separated by dots.
The first identifier is called the "package name"; the remaining part
is called the "tail". For example:

lib.posix standard Python posix module
package = "lib"
tail = "posix"

Grass.Games.Minesweeper example from my original posting
package = "Grass"
tail = "Games.Minesweeper"

2) The import statement maps a module name onto a file name in two steps.

- First, it uses the dictionary lib.sys.rename to map the package of the
calling module and the package of the requested module onto the 'true'
package name. If lib.sys.rename does not contain such a mapping, then
it uses the package of the requested module as the true package name.

- Second, it uses the true package name as an index into lib.sys.paths
to get a search path, which is a list of directory names. If no search
path exists for the true package name, an exception is raised. Otherwise,
the "tail" is converted into a relative path name by replacing dots with
slashes and appending ".py", and the search path is searched for a file
with that name.

This 'minimal system' is simpler than my original proposal, because the third
step of the lookup algorithm is gone (along with sys.path), and because the
second step is now simpler. Step 2 has been simplified in two ways:
- Each search path is now just a list of directories (except for the special
directory name "*builtin"). [In the original proposal, a search path can
also contain an absolute file name with the trailing ".py" elided. This,
I suspect, is confusing.]
- There is now a direct correspondence between package names and search paths.
Each package name has its own search path. I believe this direct
correspondence makes the system easier to reason about.

The problem with the minimal system is that it requires all module names
to have at least 2 components. All of the additional complexity in my original
proposal comes from providing full backwards compatibility, plus the
convenience of being able to write
import posix
instead of
import lib.posix