Live data from Hacker News

Ask YC: Best practice for Python module imports?

news.ycombinator.com

1–10 of 19 posts

Ask YC: Best practice for Python module imports?

#1
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()

Re: Ask YC: Best practice for Python module imports?

#2
The recommended convention in Python's own documentation is to import everything at the top, and on separate lines.

While 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.

Re: Ask YC: Best practice for Python module imports?

#4

The recommended convention in Python's own documentation is to import everything at the top, and on separate lines. While 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…

Ah ok, I wasnt aware that Python had an opinion, thats certainly (in my book anyway) a considerable argument for that approach then!

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 ...

Re: Ask YC: Best practice for Python module imports?

#5

The recommended convention in Python's own documentation is to import everything at the top, and on separate lines. While 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…

Ah ok, I wasnt aware that Python had an opinion, thats certainly (in my book anyway) a considerable argument for that approach then! 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 ap…

If you have the same 20 modules coming in over and over, consider bundling them all up in a single module of yours that does nothing but load those 20 modules, then other modules simply import that module. There's nothing wrong with that. Imports are as eligible for refactoring as anything else.

If you have a different set of 20 modules coming in over and over, you probably need to break up your modules more finely.

Re: Ask YC: Best practice for Python module imports?

#6
My own preference is on a case-by-case basis, and typically depends on whether I'm importing a module specific to my application, like a model or logic library, and whether I only need it for one specific method.

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.

Re: Ask YC: Best practice for Python module imports?

#8
post #5

Earlier quoted context omitted.

Ah ok, I wasnt aware that Python had an opinion, thats certainly (in my book anyway) a considerable argument for that approach then! 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 ap…

If you have the same 20 modules coming in over and over, consider bundling them all up in a single module of yours that does nothing but load those 20 modules, then other modules simply import that module. There's nothing wrong with that. Imports are as eligible for refactoring as anything else. If you have a different set of 20 modules coming in over and over, you probably need to break up your modules more finely.

Yeah Ive thought of that (lets call it the meta-import) approach as well (tho never tried it). I dont know why I hesitated but Im glad to hear that it seems sound.

Re: Ask YC: Best practice for Python module imports?

#9

The recommended convention in Python's own documentation is to import everything at the top, and on separate lines. While 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…

Ah ok, I wasnt aware that Python had an opinion, thats certainly (in my book anyway) a considerable argument for that approach then! 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 ap…

Ah ok, I wasnt aware that Python had an opinion

See: http://www.python.org/dev/peps/pep-0008/

Re: Ask YC: Best practice for Python module imports?

#10
My rule of thumb: I only import things locally (i.e. within a function) if it is a conditional dependency. This is particularly true if it's something not from the standard library. Example: I might have an application that supports multiple database back ends, such as PostgreSQL, SQLite, etc, but I'll conditionally import only the one applicable DB API module.
Post reply on HN