views:

224

answers:

5

The PEP 8 recommends that modules be imported at the beginning of programs.

Now, I feel that importing some of them at the beginning of the main program (i.e., after if __name__ == '__main__') makes sense. For instance, if the main program reads arguments from the command line, I tend to do import sys at the beginning of the main program: this way, sys does not have to be imported when the code is used as a module, since there is no need, in this case, for command line argument access.

How bad is this infringement to PEP 8? should I refrain from doing this? or would it be reasonable to amend PEP 8?

+5  A: 

I can't really tell you how bad this is to do.

However, I've greatly improved performance (response time, load) for a web app by importing certain libraries only at the first usage.

BTW, the following is also from PEP 8:

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

Prody
If you're really concerned about that you might want to have a look at demandimport: http://hg.intevation.org/mercurial/crew/file/tip/mercurial/demandimport.py
tonfa
I don't see how it would help response time. Startup time for sure
gnibbler
@tonfa, wow thanks a lot, I didn't know about that one
Prody
@gnibbler: it does the same thing as if import happened only at the first usage, so if it helps response time for Prody, demandimport will help in the same way.
tonfa
+2  A: 

I would recommend you to do what you feel is most appropriate when there is nothing in PEP about your concern.

Deniz Dogan
+1  A: 

Importing sys doesn't really take that long that I would worry about it. Some modules do take longer however. I don't think sys really clogs up the namespace very much. I wouldn't use a variable or class called sys regardless.

If you think it's doing more harm than good to have it at the top, by all means do it however you like. PEP 8 is just a guide line and lots of code you see does not conform to it.

gnibbler
+4  A: 

In general I don't think there's much harm in late importing for modules that may not be needed.

However sys I would definitely import early, at the top. It's such a common module that it's quite likely you might use sys elsewhere in your script and not notice that it's not always imported. sys is also one of the modules that always gets loaded by Python itself, so you are not saving any module startup time by avoiding the import (not that there's much startup for sys anyway).

bobince
+2  A: 

The issue isn't performance.

The issue is clarity.

Your "main" program is only a main program today. Tomorrow, it may be a library included in some higher-level main program. Later, it will be just one module in a bigger package.

Since your "main" program's life may change, you have two responses.

  1. Isolate the "main" things inside if __name__ == "__main__". This is not a grotesque violation of PEP-8. This is a reasonable way to package things.

  2. Try to limit the number of features in your "main" program scripts. Try to keep them down to imports and the if __name__ == "__main__" stuff. If your main script is small, then your import question goes away.

S.Lott