Setting up Python, install one module as an add-on module of another module?

I would like to create a package that installs into a submodule of another python package as an extension.

Basically, the source module is configured like this:

origmodule/ __init__.py stuff.py ext/ __init__.py # This module is empty 

Then add my extension module to an empty origmodule.ext module. Reading the instructions for distutils , it was unclear whether this is possible or supported. The ultimate goal is for my extension module to be imported after installation as follows:

import origmodule.ext.my_extension

+4
source share
1 answer

You cannot do this with setuptools, installation inside another package is not supported.

Instead, you want to use entry points instead. Your origmodule should look for something registered as an entry point for a particular key, and your extension should register for that key.

Your extension registers the extension point:

 entry_points={ 'some.opaque.string.unique.to.origmodule': ['my_extension = my.extension.package:some.entry.point', ] } 

which your origmodule can detect by setting pkg_resources :

 import pkg_resources for entrypoint in pkg_resources.iter_entry_points(group='some.opaque.string.unique.to.origmodule'): extension = entrypoint.load() 

As an example, consider the Babylonian project . Babel can extract translated text from files; he knows how to do this for Python source code, but also supports extensions that can extract such text from other formats.

Such production methods can be recorded as expansion points. Babylon documents this in the Methods for Extracting Files section. The message retrieval code then loads these input points when extracting the text:

 GROUP_NAME = 'babel.extractors' # ... for entry_point in working_set.iter_entry_points(GROUP_NAME, method): func = entry_point.load(require=True) break 

Maco template language provides such a plugin; it registers an entry point that points to the actual implementation:

 [babel.extractors] mako = mako.ext.babelplugin:extract 
 def extract(fileobj, keywords, comment_tags, options): # ... 
+5
source

All Articles