views:

244

answers:

4
  1. Do I need always import all I need in the beginning of the script?
  2. Use import immediately before using things from this?
  3. What to do if import thing uses several times?

What is good style of using import in Python?

A: 

I usually put my imports at the top of scripts: this way, I know my dependencies and I don't have to go through the whole script.

Importing several times the same module/package is no problem, functionally speaking, but incurs a small overhead past the first time.

When I write multiprocessing applications, I will do my imports a bit differently though: do only the required imports on one side of the fork() and do the rest on the other side.

jldupont
+10  A: 

From the PEP8 style guide:

  • Imports should usually be on separate lines, e.g.:

Yes: import os import sys

No: import sys, os

it's okay to say this though:

from subprocess import Popen, PIPE

  • Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Imports should be grouped in the following order:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

You should put a blank line between each group of imports.

Put any relevant all specification after the imports.

  • Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that PEP 328 [7] is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.

  • When importing a class from a class-containing module, it's usually okay to spell this

from myclass import MyClass

from foo.bar.yourclass import YourClass

If this spelling causes local name clashes, then spell them

import myclass

import foo.bar.yourclass

and use "myclass.MyClass" and "foo.bar.yourclass.YourClass"

Dominic Bou-Samra
+1: Reference PEP-8.
S.Lott
+3  A: 
  1. Do I need always import all I need in the beginning of the script?

    You don't need to, but it's considered good style (like #include statements in C/C++). It has the advantage that you don't have to scroll/search through the whole file to find imported modules.

  2. Use import immediately before using things from this?

    This is sometimes done inside functions in order to hide the import from the module namespace (or for deferred loading). But I've never found a good use case for that.

  3. What to do if import thing uses several times?

    Python takes care of that. A module won't be imported twice unless you hack sys.modules or use reload(...).

In general, you should use from .localsubmodule import a, b, c for local imports (from ..parentmodule is also possible) and import os or from os import path for global imports, for example. You can look at PEP8 - Style Guide for Python Code for more examples and suggestions.

When creating modules, make sure they are from ... import "compatible", i.e. define the __all__ variable so that you only export the objects/submodules that should be public.

AndiDog
A: 

There are definitely reasonable cases to lazy-load imports. Rare, but they exist. In these case, I like to find them at the top of a function that first requires an import..

royal
What would those cases be?
Craig McQueen
If you're writing a library that provides conditional support for things. For instance, a web framework that can work with Jinka or Mako.. it would kind of suck if you had to have both installed in order to run it.. Another example might be when a module is conditionally necessary, and expensive to import... You could argue the module is poorly designed, and that argument would probably hold true.. but it's still reasonable if you find yourself in the position of needing to occasionally import a poorly written module.
royal