mkinit package

Subpackages

Submodules

Module contents

The MkInit Module

A tool to autogenerate explicit top-level imports

Read the docs

https://mkinit.readthedocs.io

Github

https://github.com/Erotemic/mkinit

Pypi

https://pypi.org/project/mkinit

Autogenerates __init__.py files statically and dynamically. To use the static version simply pip install and run the mkinit command with the directory corresponding to the package.

The main page for this project is: https://github.com/Erotemic/mkinit

Quick Start

Install mkinit via pip install mkinit. Then run:

MOD_INIT_PATH=path/to/repo/module/__init__.py
mkinit "$MOD_INIT_PATH"

This will display autogenerated code that exposes all top-level imports. Use the --diff option to check differences with existing code, and add the -w flag to write the result.

mkinit.autogen_init(modpath_or_name, submodules=None, respect_all=True, options=None, dry=False, diff=False, recursive=False)[source]

Autogenerates imports for a package __init__.py file.

Parameters:
  • modpath_or_name (PathLike | str) – path to or name of a package module. The path should reference the dirname not the __init__.py file. If specified by name, must be findable from the PYTHONPATH.

  • submodules (List[str] | None, default=None) – if specified, then only these specific submodules are used in package generation. Otherwise, all non underscore prefixed modules are used.

  • respect_all (bool, default=True) – if False the __all__ attribute is ignored while parsing.

  • options (dict | None) – formatting options; customizes how output is formatted. See formatting._ensure_options for defaults.

  • dry (bool, default=False) – if True, the autogenerated string is not written

  • recursive (bool, default=False) – if True, we will autogenerate init files for all subpackages.

Note

This will partially override the __init__ file. By default everything up to the last comment / __future__ import is preserved, and everything after is overriden. For more fine grained control, you can specify XML-like # <AUTOGEN_INIT> and # </AUTOGEN_INIT> comments around the volitle area. If specified only the area between these tags will be overwritten.

To autogenerate a module on demand, its useful to keep a doctr comment in the __init__ file like this

python -m mkinit <your_module>

Example

>>> init_fpath, new_text = autogen_init('mkinit', submodules=None,
>>>                                     respect_all=True,
>>>                                     dry=True)
>>> assert 'autogen_init' in new_text
mkinit.dynamic_init(modname, submodules=None, dump=False, verbose=False)[source]

Main entry point for dynamic mkinit.

Dynamically import listed util libraries and their attributes. Create reload_subs function.

Using __import__ like this is typically not considered good style However, it is better than import * and this will generate the good file text that can be used when the module is ‘frozen”

Note

Dynamic mkinit is for initial development and prototyping, and even then it is not recommended. For production it is strongly recommended to use static mkinit instead of dynamic mkinit.

Example

>>> # The easiest way to use this in your code is to add these lines
>>> # to the module __init__ file
>>> from mkinit import dynamic_init
>>> execstr = dynamic_init('mkinit')
>>> print(execstr)
>>> exec(execstr)  # xdoc: +SKIP
mkinit.static_init(modpath_or_name, submodules=None, respect_all=True, options=None)[source]

Returns the autogenerated initialization string. This can either be executed with exec or directly copied into the __init__.py file.