A Python wish: an easy, widely supported way to turn a path into a module
Years ago I wrote a grumpy entry about Django 1.4's restructured directory layout and mentioned that I was not reorganizing our Django web app to match. In the time since then, it has become completely obvious that grimly sticking to my guns here is not a viable answer over the long term; sooner or later, ideally sooner, I need to restructure the app into what is now the proper Django directory layout.
One of the reasons that I objected to this (and still do)
is the problem of how you make a directory into a module; simply adding the parent directory to
$PYTHONPATH has several limitations. Which is where my wish comes in.
What I wish for is a simple and widely supported way to say 'directory
/some/thing/here/fred-1 is the module
fred'. This should be
supported on the Python command line, in things like
and in Python code itself (so you could write code that programmatically
added modules this way, similar to how you can extend
in code). The named module is not imported immediately, but if later
code does '
import fred' (or any of its variants) it will be loaded
/some/thing/here/fred-1 (assuming that there is no other
fred module found earlier on the import path). All of the usual
things work from there, such as importing submodules ('
fred.bob') and so on. The
fred-1 directory would have a
__init__.py and so on as normal.
(Note that it is not necessary for the final directory name to be
the same as the module name. Here we have a directory being
but the module is
Given PEP 302, I think it's probably possible to implement this in Python code. However, both PEP 302 and the entire current Python import process make my head hurt so I'm not sure (and there are probably differences between Python 2 and Python 3 here).
(I wrote some notes to myself about Python packaging a while back, which is partly relevant to
this quest. I don't think
.zip files let me do what I
want here, even if I was willing to pack things up in
I believe their filenames are bound to the package/module name.)