Tests pypi version Code style: black pdm-managed

Control the exported members for your modules


Modul requires Python >=3.7


$ python -m pip install modul

Modul is a single-file module with less than 200 lines of code and no dependencies. It can be easily copied into your project.

Quick start

Write a module exporting limited members:

from modul import exports

def foo():
    return 42

baz = "unexported"
bar = "hello" = bar

In another module or REPL:

>>> import mymodule
>>> mymodule.baz
AttributeError: Module test has no attribute baz
>>> mymodule.__all__
['foo', 'bar']


  1. Export a function with decorator:

    def foo():
        return 42
  2. Export a variable with attribute set: = 42

    Note that to use the variable inside the module, you still need to declare a variable for it:

    bar = 42 = bar
  3. Export a variable with item set:

    exports["bar"] = 42

    Besides, the exports object supports all APIs of dict:

    exports.update({"bar": 42})
  4. Export a map of (name, value) pairs:

        "bar": 42,
        "baz": "hello"
  5. You can even have conditional exports and exports from function call:

    flag = True
    if flag: = 42
    def export_bar(): = 42
  6. Alternatively, you can assign members to the exports attribute of the module:

    import modul
    modul.exports = {
        "bar": 42,
        "baz": "hello"

    Note that you can’t use exports = <variable> in this case, because it will lose the reference to the API. And each assignment will overwrite the previous one so there can be only one assignment in your module.


View Github