imports after function definitions?

Peter Hansen peter at
Fri Aug 29 19:40:49 CEST 2003

Hallvard B Furuseth wrote:
> Terry Reedy wrote:
> >in message news:HBF.20030828usgy at
> >> Is there any reason not to structure my program like this?
> >>
> >>    def ...
> >>    def ...
> >>    var = ...
> >>    var = ...
> >>    import ...
> >>    import ...
> >>    main_function()
> >
> > It is helpful to human readers to see dependencies at the top and to
> > know what globals are avaiable when reading defs.  What would be
> > compensating gain of doing such?
> I'm setting sys.path in a config function which depends on variables
> near the end of the file.  So the config function must be called late,
> and the imports that depend on it after that.

Sounds like the structure ought to be changed (IMHO).  This is 
an unusual arrangement, but not a highly unusual thing to need to do.
Why not just put the sys.path-setting stuff, including the variables
on which it seems to depend (I'd ask why that's need, too), in a
separate module, possibly the __main__ one, and have the rest of 
the code including the stuff that needs the imports be called 
from there.

In other words, why do you need all those defines and all those
variables to come before the imports?  Are they really all required
for the sys.path-setting code?  If so, shouldn't they just be in
a separate module?


More information about the Python-list mailing list