Ask YC: Best practice for Python module imports?
Im torn between two general module import styles. The import-everything-at-the-top approach where the beginning of each python file/module imports every module potentially needed later in the file OR the import-as-needed approach.
I know what I dont like; a file starting w/ 20+ import lines, that just seems ugly and java-y (no-flame, just sayin) to me. OTOH, the as-needed-approach seems perhaps a little too, uhm, disorganized? Although Id guess Im probably more partial to that approach.
This is a pretty minor stylistic issue but I havent settled on one approach and I'd like some feedback to come to some conclusion once and for all.
For example,
import foo import baz import bar import quux import django.foo # etc
vs.
import sys
def some_os_thing(): import os print os.getcwd()
def some_http_thing(): import urllib print urllib.urlopen('http://...').read()
18 comments
[ 3.2 ms ] story [ 47.0 ms ] threadWhile technically Python doesn't import anything twice, it still takes nonzero time to check the module registry (a dictionary lookup). So if this happens every time your functions are called, it's unnecessary work, even if it's fast.
I personally tend to do all imports in one place, with the exception of test routines: I figure that a user of a module shouldn't really "depend" on modules that are only needed in test mode, so e.g. I would do a local "import" inside a _test() function.
I think I might be converging on a reasonable practice of doing all imports in one place while including basic comments and/or just insuring at least that imports are grouped by category / 'package'.
# Sys imports import os, sys, urllib
# django imports import django.foo ...
# google apps imports import gdata.foo ...
If you have a different set of 20 modules coming in over and over, you probably need to break up your modules more finely.
If it is the same set of 20 modules, but those twenty modules are not conceptually similar, that might be something worth investigating further. That those modules need to be imported might suggest that the current module is attempting to do too many things at once.
I only point this out because a former coworker's code I've been dealing with. This was in PHP, but everything had been broken out into classes and the classes were named after the files. So, closer than just including raw PHP code into the same scope, and hopefully close enough that I don't get buried in comments to the contrary. :-)
But the coworker had the logical extreme of a module importing module. Arguably, he had the same 120 files coming in over and over. But this created all sorts of problems, the most prevalent being that a typo anywhere in these module files would bring down the entire site, since they have to be syntax checked.
So, I overall agree with jerf, with the addendum of be vigilant and be careful. :-)
See: http://www.python.org/dev/peps/pep-0008/
The best reason to do them at the top of a module, at least to me, is so you know where all the imports are. The only reason I'd ever put them in functions is to avoid circular import issues.
There's really no need to use comments; reading the module should be enough to figure out what the grouping is. My only convention is sticking all the global imports in one line at the top (PEP be damned) and grouping stuff from there.
Edit: it is also non-trivial from a performance point of view to have a module import every time a function is called...from x import y
inside any function that needs y.
OTOH I think there are good reasons to go with the official Python recommendation, which another poster says states that all imports should be at the top on separate lines.
Standard libraries I'm more likely to import globally, since I'm more likely to want to use them several times.
The most important thing is to figure out what works for you, and then be consistent about it.
There's a section on imports.
a, use your head, don't let convention override reason b, In general, keep module imports at the top of the file organized by type & package with the caveat that a large # of imports (say more than 10 or 20) may indicate the module is too ambitious and is a candidate for refactoring.
I've learned a lot by looking at the source code for guido's rietveld: http://code.google.com/p/rietveld/source/browse/trunk/codere...