Live data from Hacker News

Ask YC: Best practice for Python module imports?

news.ycombinator.com

11–19 of 19 posts

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

#12

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…

PEP 8 has an opinion on everything ;)

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.

  import os, sys, datetime, xmlrpclib
  from django.http import Htt404, HttpResponseRedirect
  from django.core import serializers
  from tstumbler.util import slice_it, parse_date, flatten
Edit: it is also non-trivial from a performance point of view to have a module import every time a function is called...

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

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

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.

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

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

#14
post #5

Earlier quoted context omitted.

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.

Also note, google app engine throws a warning if try and import anywhere but at the top.

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

#15

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…

You might also want to take advantage of:

  from foo import bar, baz, quux
if you aren't already.

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

#17
Ok I appreciate the helpful discussion, this will inform my future practice. To wrap up, I think the takeaways are:

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.

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

#18

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…

Ah ok, I wasnt aware that Python had an opinion See: http://www.python.org/dev/peps/pep-0008/

Agreed. Unless there's a compelling reason not to (conditional imports), it's definitely best to stick with Python conventions (a la PEP8). These conventions are what keep Python one of the cleanest and most readable languages out there.

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

#19
One reason to import something within a function would be for lazy loading. If I do end up having many different imports, I tend to group them by library/framework.

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

Post reply on HN