views:

848

answers:

6

I've tried to find a comprehensive guide on whether it is best to use 'import module' or 'from module import'. I've just started with Python, with the intention for developing web applications with Django, and I'm trying to start off with best practices in mind.

Basically, I was hoping if anyone could share their experiences, what preferences other developers have and whats the best way to avoid any gotchas down the road.

+10  A: 

Both ways are supported for a reason: there are times when one is more appropriate than the other.

import module: nice when you are using many bits from the module. drawback is that you'll need to qualify each reference with the module name.

from module import ...: nice that imported items are usable directly without module name prefix. drawback is that you must list each thing you use, and that it's not clear in code where something came from.

Which to use depends on which makes the code clear and readable, and has more than a little to do with personal preference. I lean toward import module generally because in the code it's very clear where an object or function came from. I use from module import ... when I'm using some object/function a lot in the code.

dwc
+18  A: 

The difference between 'import module' and 'from module import foo' is subjective. Pick the one you like best and be consistent in your use of it. Here are some points to help you decide on which you prefer

  • 'import module'
    • Pros: less maintenance of your import statement code (don't have to update import statement if you start using another item from the module)
    • Cons: typing module.foo in your code can be tedious and redundant
  • 'from module import foo'
    • Pros: Don't have to type as much to use foo
    • Cons: Have to update your import statement every time you use a new item from the module

Either method is acceptable, but don't use 'from module import *'. For any reasonable large set of code, if you 'import *' your will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it east to get to the point where you think you don't use the import anymore but its extremely difficult to be sure.

Mark Roddy
+1 for talking about 'from module import *'
dwc
+1 for discouraging usage of "from module import *", it just clutters the namespace.
Christian Witts
cluttering the namespace is *not* the most problematic part of "import *", it's the reduction in readability: Any name conflicts will show themselves in (unit) testing. But all the names you use from the imported module will be bare, with nary a hint were they come from. I absolutely loathe "import *".
jae
+1  A: 
import module

Is best when you will use many functions from the module.

from module import function

Is best when you want to avoid polluting the global namespace with all the functions and types from a module when you only need function.

Andrew Hare
Surely the only thing in the global namespace if you do 'import module' is 'module' ? You only pollute the namespace if you do 'from .. import *'.
John Fouhy
Yes good point - I could have made that clearer.
Andrew Hare
+8  A: 

I personally always use

from package.subpackage.subsubpackage import module

and then access everything as

module.function
module.modulevar

etc. The reason is that at the same time you have short invocation, and you clearly define the module namespace of each routine, something that is very useful if you have to search for usage of a given module in your source.

Needless to say, do not use the import *, because it pollutes your namespace and it does not tell you where a given function comes from (from which module)

Of course, you can run in trouble if you have the same module name for two different modules in two different packages, like

from package1.subpackage import module
from package2.subpackage import module

in this case, of course you run into troubles, but then there's a strong hint that your package layout is flawed, and you have to rethink it.

Stefano Borini
In the last case, you can always use: import pkgN.sub.module as modNgiving you distinct names for each module. You can also use the 'import modulename as mod1' pattern to shorten a long name, or to switch between implementations of the same API (e.g. DB API modules) with a single name change.
Jeff Shannon
A: 

To add to what people have said about from x import *: besides making it more difficult to tell where names came from, this throws off code checkers like Pylint. They will report those names as undefined variables.

DNS
+1  A: 

My own answer to this depends mostly on first, how many different modules I'll be using. If i'm only going to use one or two, I'll often use from ... import since it makes for fewer keystrokes in the rest of the file, but if I'm going to make use of many different modules, I prefer just import because that means that each module reference is self-documenting. I can see where each symbol comes from without having to hunt around.

Usuaully I prefer the self documenting style of plain import and only change to from.. import when the number of times I have to type the module name grows above 10 to 20, even if there's only one module being imported.

TokenMacGuy