views:

328

answers:

5

Does it import __init__.py found in the containing folder?

e.g., is it necessary to declare from project.model import __init__

or is from project.model import * sufficient?

+3  A: 

Yes, it does. It imports everything (that is not a private variable, i.e.: variables whose names start with _ or __), and you should try not to use it to avoid polluting the local namespace.

It is enough, but generally you should either do import project.model which already imports __init__.py, but can get too wordy if you use it too much, or import project.model as pm or import project.model as model to save a few keystrokes later on when you use it.

Listen to Alex's advices.

voyager
Good recommendation, but imprecise information: `from foo import *` does **not** "import everything" -- it imports names listed in the module's `__all__` attribute, or, missing that attribute, _non-private_ names (excluding names starting with `_`).
Alex Martelli
@Alex, good point. Would you like to expand on it, or would you prefer that I do it?
voyager
@voyager, I've already expanded on it -- see my answer to this question.
Alex Martelli
Just saw it, upvoted and linked.
voyager
+2  A: 

If project.model is a package, the module referred to by import project.model is from .../project/model/__init__.py. from project.model import * dumps everything from __init__.py's namespace into yours. It does not automatically do anything with the other modules in model. The preferred style is for __init__.py not to contain anything.

Never ever ever ever ever use import *. It makes your code unreadable and unmaintainable.

Mike Graham
+1  A: 

if the module in question (project.model in your case) has defined a list of stings named __all__, then every named variable in that list is imported, if there is no such variable, it imports everything.

TokenMacGuy
+6  A: 

It import (into the current namespace) whatever names the module (or package) lists in its __all__ attribute -- missing such an attribute, all names that don't start with _.

It's mostly intended as a handy shortcut for use only in interactive interpreter sessions: as other answers suggest, don't use it in a program.

My recommendation, per the Google's Python style guide, is to only ever import modules, not classes or functions (or other names) from within modules. Strictly following this makes for clarity and precision, and avoids subtle traps that may come when you import "stuff from within a module".

Importing a package (or anything from inside it) intrinsically loads and executes the package's __init__.py -- that file defines the body of the package. However, it does not bind the name __init__ in your current namespace (so in this sense it doesn't import that name).

Alex Martelli
+6  A: 

The "advantage" of from xyz import * as opposed to other forms of import is that it imports everything (well, almost... [see (a) below] everything) from the designated module under the current module. This allows using the various objects (variables, classes, methods...) from the imported module without prefixing them with the module's name. For example

>>> from math import *
>>>pi
3.141592653589793
>>>sin(pi/2)
>>>1.0

This practice (of importing * into the current namespace) is however discouraged because it it

  • provides the opportunity for namespace collisions (say if you had a variable name pi prior to the import)
  • may be inefficient, if the number of objects imported is big.
  • doesn't explicitly document the origin of the variable/method/class (it is nice to have this "self documentation" of the program for future visit into the code)

Typically we therefore limit this import * practice to ad-hoc tests and the like, and instead:

explicitly import a few objects only

>>>from math import pi
>>>pi
>>>3.141592653589793
>>> sin(pi/2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sin' is not defined

or import the module under its own namespace (or an alias thereof, in particular if this is a long name, and the program references its objects many times)

  >>>import math
  >>>math.pi
  >>>3.141592653589793
  etc..


  >>>import math as m  #bad example math being so short and standard...
  >>>m.pi
  >>>3.141592653589793
  etc..

See the Python documentation on this topic

(a) Specifically, what gets imported with from xyz import *
if xyz module defines an __all__ variable, it will import all the names defined in this sequence, otherwise it will import all names, except these which start with one underscore.

mjv
The advantage of `from X import *` is that it allows you to be lazy. The problem of it is that it will bite you in the ass for lazy :)
voyager