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?

Eric O Lebigot
  • 81,422
  • 40
  • 198
  • 249
  • optparse is available for command-line parsing, better than using sys.argv and doing your own parsing. – monkut Oct 14 '09 at 11:59
  • 1
    Related: http://stackoverflow.com/questions/1024049/is-it-pythonic-to-import-inside-functions – codeape Oct 14 '09 at 12:11
  • @monkut: optparse is not especially useful if you don't use command line *options* (i.e., when you only need to handle command line arguments with no options). – Eric O Lebigot Oct 14 '09 at 13:02
  • You cannot "infringe" PEP8 because it is not a rule. It is just advice that gives you a good starting point. For instance, I often write Python code with two spaces per indent level. It's not illegal, doesn't mix tabs and spaces, and it is still quite legible. But that's not what PEP 8 advises. – Michael Dillon Oct 17 '09 at 20:06

5 Answers5

9

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!

Dominic Rodger
  • 90,548
  • 30
  • 192
  • 207
Prody
  • 5,042
  • 6
  • 41
  • 61
  • 2
    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 Oct 14 '09 at 09:43
  • 1
    I don't see how it would help response time. Startup time for sure – John La Rooy Oct 14 '09 at 09:46
  • @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 Oct 14 '09 at 10:27
6

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
  • 498,320
  • 101
  • 621
  • 807
2

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

Deniz Dogan
  • 23,833
  • 33
  • 104
  • 154
2

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.

John La Rooy
  • 263,347
  • 47
  • 334
  • 476
2

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
  • 359,791
  • 75
  • 487
  • 757