Simple Python Project Structure

Ben Finney bignose+hates-spam at benfinney.id.au
Sat Oct 11 01:20:31 EDT 2008


George Sakkis <george.sakkis at gmail.com> writes:

> '''
> The __init__.py files are required to make Python treat the
> directories as containing packages; this is done to prevent
> directories with a common name, such as "string", from
> unintentionally hiding valid modules that occur later on the module
> search path.
> '''
> 
> Is this a real problem or a speculation ? I would guess that it's at
> least as likely for a newbie to create a "string.py" module than
> have an irrelevant "string" subdirectory under a code directory
> tree. Having to create an empty file as a flag to denote a package
> doesn't seem very pythonic.

The underlying problem, of course, is that Python's ‘import’ statement
doesn't let the programmer distinguish between “import from the
system search path” versus “import from this local package”. If it
did, you could say “import ‘string’ from the system search path”
without worrying about what happens when a ‘string’ module or package
is also located in your local package.

Fortunately, this is already addressed with absolute imports versus
relative imports <URL:http://www.python.org/dev/peps/pep-0328>. See
the “Timeline” section in that PEP for what import behaviour to
expect under different Python versions.

-- 
 \      “Contentment is a pearl of great price, and whosoever procures |
  `\        it at the expense of ten thousand desires makes a wise and |
_o__)                                      happy purchase.” —J. Balguy |
Ben Finney



More information about the Python-list mailing list