Imports again...

Tim Golden mail at timgolden.me.uk
Fri Apr 9 09:56:18 CEST 2010


On 08/04/2010 14:16, Alex Hall wrote:
> The above link is to a project. I am new to using multiple files in
> Python, and I have a lot of tangled imports where many files in the
> same folder are importing each other. When I tried to follow the
> manual to make some files into packages, it did not work. Can anyone
> explain why I am getting an import error in the above project, and/or
> how I can clean up the file structure and imports to avoid problems
> like this in the future? Thanks in advance for any help, and I
> apologize for the broken link the other day.

I don't have the energy at the moment to install all the dependencies
to make the thing run, but just glancing at the layout... you appear
to have a slightly confused idea of how packages are used. The main
"arm" directory has an __init__.py as does a "modes" subdirectory and
a "weather" subdirectory of that. But neither of these has any code
in them to be imported. And the "arm" directory appears to be the main
application directory so making that into a package doesn't seem to
serve any purpose.

I'm sure there are better explanations around, but in short: packages
are directories with Python modules in them and, specifically, one Python
module __init__.py which is typically empty (but needn't be). They can
be considered in different ways, but essentially are ways of grouping
Python modules according to some idea of cohesion. They don't magically
make tangled code untangled but they might offer a certain clarity where
you'd otherwise have one big directory full of undifferentiated modules.

A typical example of packages would be a system which wanted to output
to csv, html and pdf using some standardised API. The various
output modules might live inside an "outputs" subpackage so the main
program could do "from outputs import csv" or whatever. As it happens,
this example also shows that the "outputs.csv" module won't shadow the
stdlib csv module -- also purists might argue that it's bad practice to
name any module over a stdlib module.

Creating a package in your case might help you -- I haven't really looked
at your code enough to say for sure -- to break one big module into several
more structured modules within a package.

TJG



More information about the Python-list mailing list