246

My package has the following structure:

mobilescouter/
    __init__.py #1
    mapper/
        __init__.py  #2
        lxml/
            __init__.py #3
            vehiclemapper.py
            vehiclefeaturemapper.py
            vehiclefeaturesetmapper.py
        ...
        basemapper.py
   vehicle/
        __init__.py #4
        vehicle.py
        vehiclefeature.py
        vehiclefeaturemapper.py
   ...

I'm not sure how the __init__.py files should be correctly written.
The __init__.py #1 looks like:

__all__ = ['mapper', 'vehicle']
import mapper
import vehicle

But how should for example __init__.py #2 look like? Mine is:

__all__ = ['basemapper', 'lxml']
from basemaper import *
import lxml

When should be __all__ used?

2
  • 8
    Be aware though that using import * in code is generally very bad practice and should be avoided if possible. There are very few good use cases for this, but they are really rare.
    – Mayou36
    Commented Aug 21, 2017 at 7:42
  • 1
    PSA: if you're interested in learning how to write good namespace packages (the new kind of package), check out this example package: github.com/pypa/sample-namespace-packages
    – Kyle
    Commented Jun 6, 2019 at 1:57

3 Answers 3

188

__all__ is very good - it helps guide import statements without automatically importing modules http://docs.python.org/tutorial/modules.html#importing-from-a-package

using __all__ and import * is redundant, only __all__ is needed

I think one of the most powerful reasons to use import * in an __init__.py to import packages is to be able to refactor a script that has grown into multiple scripts without breaking an existing application. But if you're designing a package from the start. I think it's best to leave __init__.py files empty.

for example:

foo.py - contains classes related to foo such as fooFactory, tallFoo, shortFoo

then the app grows and now it's a whole folder

foo/
    __init__.py
    foofactories.py
    tallFoos.py
    shortfoos.py
    mediumfoos.py
    santaslittlehelperfoo.py
    superawsomefoo.py
    anotherfoo.py

then the init script can say

__all__ = ['foofactories', 'tallFoos', 'shortfoos', 'medumfoos',
           'santaslittlehelperfoo', 'superawsomefoo', 'anotherfoo']
# deprecated to keep older scripts who import this from breaking
from foo.foofactories import fooFactory
from foo.tallfoos import tallFoo
from foo.shortfoos import shortFoo

so that a script written to do the following does not break during the change:

from foo import fooFactory, tallFoo, shortFoo
4
  • 5
    I was very confused about 'all' and line by line import. Your example is very illuminating.
    – Junchen
    Commented Nov 29, 2016 at 15:25
  • 3
    I'm confused by "__all__ and import * is redundant", __all__ is used by the consumer of the module, and from foo import * is used by the module itself to use others....
    – Nick T
    Commented Jan 25, 2019 at 18:10
  • 1
    using __all__ and import * is redundant, only __all__ is needed How are those redundant? They do different things.
    – endolith
    Commented Sep 10, 2019 at 4:08
  • What about importing variables from, let's say, an __version__.py
    – theX
    Commented Oct 2, 2020 at 18:55
127

My own __init__.py files are empty more often than not. In particular, I never have a from blah import * as part of __init__.py -- if "importing the package" means getting all sort of classes, functions etc defined directly as part of the package, then I would lexically copy the contents of blah.py into the package's __init__.py instead and remove blah.py (the multiplication of source files does no good here).

If you do insist on supporting the import * idioms (eek), then using __all__ (with as miniscule a list of names as you can bring yourself to have in it) may help for damage control. In general, namespaces and explicit imports are good things, and I strongly suggest reconsidering any approach based on systematically bypassing either or both concepts!-)

6
  • 12
    Personally, I prefer to keep things separate, and then import *. THe reason is that, despite folding and stuff, I still hate to browse files containing too many classes, even if related. Commented Dec 22, 2009 at 7:45
  • 6
    @stefano think about a big framework. if it uses import * you must unconditionally accept all the framework in its all, even features the you will never use. keeping __init__.py empty give you more chances than just all-or-nothing semantic. think about twisted.
    – mg.
    Commented Dec 22, 2009 at 9:53
  • if keep it empty, even after import mobilescouter, one still can't use mobilescouter.mapper or mobilescouter.vehicle or mobilescouter.whatever. isn't import mobilescouter.A, mobilescouter.B..... too verbose?
    – sunqiang
    Commented Dec 22, 2009 at 13:08
  • 7
    @sunqiang this is personal but i don't think so. from mobilescouter import A, B is just a line of code and you don't have a project with 666 classes and every one with his own file, right? if you have two or more import * in your code you are filling the namespace with potential garbage and quickly you'll forget where A come from. And if an upper package do the same? you are grabbing all the sub-packages and sub-sub-packages. like the zen of python says, explicit is better than implicit.
    – mg.
    Commented Dec 22, 2009 at 14:46
  • 2
    @mg, if there is a line "import A, B" in the init.py file, then I can call the A(or B) with the syntax:mobilescouter.A; if we use "from mobilescouter import A, B", then it's just A.something. sometime just this line, I don't remember A is a sub pacakge of mobilescouter, and I think this contributes to namespace pollution (though it's a lot better than ""from mobilescouter import *". I still prefer "import pkgname" give user the uniform public interface. so init.py do the import sub_pkgname things.
    – sunqiang
    Commented Dec 22, 2009 at 15:24
6

Your __init__.py should have a docstring.

Although all the functionality is implemented in modules and subpackages, your package docstring is the place to document where to start. For example, consider the python email package. The package documentation is an introduction describing the purpose, background, and how the various components within the package work together. If you automatically generate documentation from docstrings using sphinx or another package, the package docstring is exactly the right place to describe such an introduction.

For any other content, see the excellent answers by firecrow and Alex Martelli.

2
  • Does the actual __init__.py for the email package follow this guideline? I see a single line docstring that doesn't do much to explain "how the various components within the package work together".
    – Gertlex
    Commented Oct 11, 2019 at 6:07
  • @Gertlex Maybe only in the web documentation.
    – gerrit
    Commented Oct 11, 2019 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.