241

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?

schmi
  • 397
  • 3
  • 15
Marten Bauer
  • 2,413
  • 3
  • 15
  • 4
  • 7
    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 Aug 21 '17 at 07: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: https://github.com/pypa/sample-namespace-packages – Kyle Jun 06 '19 at 01:57

3 Answers3

183

__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
Fire Crow
  • 7,499
  • 4
  • 36
  • 35
  • 4
    I was very confused about '__all__' and line by line import. Your example is very illuminating. – Junchen Nov 29 '16 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 Jan 25 '19 at 18:10
  • 1
    `using __all__ and import * is redundant, only __all__ is needed` How are those redundant? They do different things. – endolith Sep 10 '19 at 04:08
  • What about importing variables from, let's say, an `__version__.py` – theX Oct 02 '20 at 18:55
126

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!-)

codeforester
  • 39,467
  • 16
  • 112
  • 140
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 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. – Stefano Borini Dec 22 '09 at 07: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. Dec 22 '09 at 09: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 Dec 22 '09 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. Dec 22 '09 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 Dec 22 '09 at 15:24
  • What if I wanted to somehow highlight with the code, that the whole package is dedicated to public just one function/class to it's outside? Is it also bad to specify it in packages' __all__ then? If so, what would be the way to do it then? – tikej Oct 26 '20 at 15:28
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.

gerrit
  • 24,025
  • 17
  • 97
  • 170
  • Does the actual [`__init__.py`](https://github.com/python/cpython/blob/3.7/Lib/email/__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 Oct 11 '19 at 06:07
  • @Gertlex Maybe only in the web documentation. – gerrit Oct 11 '19 at 11:11