Search code examples
pythonhooksetuptools

Examples of entry_point usage


I discoverd entry_points of setuptools:

http://pythonhosted.org/setuptools/setuptools.html#dynamic-discovery-of-services-and-plugins

quote: setuptools supports creating libraries that “plug in” to extensible applications and frameworks, by letting you register “entry points” in your project that can be imported by the application or framework.

But I have not seen a project using them.

Are there examples of projects which use them?

If not, why are they not used?


Solution

  • There are loads of examples. Any project that defines console scripts uses them, for example. A quick search on GitHub gives you plenty to browse through.

    I'll focus on one specific example (one that is not on GitHub): Babel.

    Babel uses both entry_points for both console scripts and to define extension points for translatable text extraction. See their setup.py source:

    if have_setuptools:
        extra_arguments = dict(
            zip_safe = False,
            test_suite = 'babel.tests.suite',
            tests_require = ['pytz'],
    
            entry_points = """
            [console_scripts]
            pybabel = babel.messages.frontend:main
    
            [distutils.commands]
            compile_catalog = babel.messages.frontend:compile_catalog
            extract_messages = babel.messages.frontend:extract_messages
            init_catalog = babel.messages.frontend:init_catalog
            update_catalog = babel.messages.frontend:update_catalog
    
            [distutils.setup_keywords]
            message_extractors = babel.messages.frontend:check_message_extractors
    
            [babel.checkers]
            num_plurals = babel.messages.checkers:num_plurals
            python_format = babel.messages.checkers:python_format
    
            [babel.extractors]
            ignore = babel.messages.extract:extract_nothing
            python = babel.messages.extract:extract_python
            javascript = babel.messages.extract:extract_javascript
            """,
        )
    

    Tools like pip and zc.buildout use the console_scripts entry point to create commandline scripts (one called pybabel, running the main() callable in the babel.messages.frontend module).

    The distutils.commands entry points defines additional commands you can use when running setup.py; these can be used in your own projects to invoke Babel command-line utilities right from your setup script.

    Last, but not least, it registers its own checkers and extractors. The babel.extractors entry point is loaded by the babel.messages.extract.extract function, using the setuptools pkg_resources module, giving access to all installed Python projects that registered that entry point. The following code looks for a specific extractor in those entries:

    try:
        from pkg_resources import working_set
    except ImportError:
        pass
    else:
        for entry_point in working_set.iter_entry_points(GROUP_NAME,
                                                         method):
            func = entry_point.load(require=True)
            break
    

    This lets any project register additional extractors; simply add an entry point in your setup.py and Babel can make use of it.