The problem with trying to make everything into a Python module
One of the reasons for Django's unpleasant project restructuring is that they want your website directory (ie the directory that your project sits in) to be a module that can be imported. This in fact seems to be somewhat of a general trend; all sorts of things rather want you to to have not just a collection of files in a directory but an actual module. I wish they'd stop. Modules are not the be all and end all in Python, at least not as currently implemented, and not everything needs or wants to be a module.
The general reason for making things into modules is namespaces for
imports. If you're sitting in your project's directory and do 'import
fred
', in theory this is ambiguous; you might mean your fred.py
or you might mean some global fred
module installed in Python. The
absolute form of 'import mystuff.fred
' is more or less unambiguous.
(This preference for modules also goes with the fact that the relative
import syntax, 'from . import fred
', is only valid in an actual
module. I think that this is a terrible mistake, but no one asked me for
my opinion.)
I have no problem with modules as such. The problem I have is how you
get a directory to be a module, namely that you add the directory's
parent to the Python search path (in one of a number of ways), and
then the directory becomes a module (or technically I think a package)
called its directory name. This is bad in at least two ways. It tightly
couples together the directory name and the module name and it also
makes everything else in the directory's parent available as a potential
module. What both of these have in common is undesired name collisions.
For example, you cannot be working on two versions of a 'fred' module
that are sitting in a directory as, say, src/fred-1
and src/fred-2
,
not unless you want to have a src/fred
symlink that you keep changing
back and forth.
(The natural structure seems to be to isolate each module in its own
artificial parent directory (eg src/fred-1/fred
) or to ignore the
whole issue, put everything in src/
, and assume you will never have
any collisions or be developing a new version of fred that you don't
want src/bob
getting when it does an 'import fred
'.)
What would make this situation okay is a simple way to tell Python 'directory X is module Y', where 'X' might be '.' (the current directory). This should be available both on the Python command line and from inside Python code. Sadly I don't expect this to arrive any time soon.
(This stuff irritates me for reasons that are hard to pin down. Partly
it just feels wrong (eg '/src
' or wherever isn't a directory of
modules, so why am I telling Python that it is?).)
|
|