Python (and Django) best import practices

Viewed 18039

Out of the various ways to import code, are there some ways that are preferable to use, compared to others? This link http://effbot.org/zone/import-confusion.htm in short states that

from foo.bar import MyClass

is not the preferred way to import MyClass under normal circumstances or unless you know what you are doing. (Rather, a better way would like:

import foo.bar as foobaralias

and then in the code, to access MyClass use

foobaralias.MyClass

)

In short, it seems that the above-referenced link is saying it is usually better to import everything from a module, rather than just parts of the module.

However, that article I linked is really old.

I've also heard that it is better, at least in the context of Django projects, to instead only import the classes you want to use, rather than the whole module. It has been said that this form helps avoid circular import errors or at least makes the django import system less fragile. It was pointed out that Django's own code seems to prefer "from x import y" over "import x".

Assuming the project I am working on doesn't use any special features of __init__.py ... (all of our __init__.py files are empty), what import method should I favor, and why?

3 Answers
Related