``from module import *`` is discouraged in a module that contains
implementation code, as it impedes clarity and often imports unused variables.
It can, however, be used for a package that is laid out in the following
manner::
packagename
packagename/__init__.py
packagename/submodule1.py
packagename/submodule2.py
In this case, ``packagename/__init__.py`` may be::
"""
A docstring describing the package goes here
"""
from submodule1 import *
from submodule2 import *
This allows functions or classes in the submodules to be used directly as
``packagename.foo`` rather than ``packagename.submodule1.foo``. If this is
used, it is strongly recommended that the submodules make use of the ``__all__``
variable to specify which modules should be imported. Thus, ``submodule2.py``
might read::
from numpy import array, linspace
__all__ = ['foo', 'AClass']
def foo(bar):
# the function would be defined here
pass
class AClass(object):
# the class is defined here
pass
This ensures that ``from submodule import *`` only imports ``':func:`foo'``
and ``':class:`AClass'``, but not ``':class:`numpy.array'`` or
``':func:`numpy.linspace'``.
Acknowledgements
================
The present document's coding guidelines are derived from project `Astropy's codding guidelines <http://docs.astropy.org/en/stable/development/codeguide.html>`_.