PyUtilib / pyutilib

A collection of general Python utilities, including logging and file IO, subprocess management, plugin systems, and workflow management.
BSD 3-Clause "New" or "Revised" License
34 stars 21 forks source link

[PCA] cannot load implementation from other file #24

Closed geraldebberink closed 7 years ago

geraldebberink commented 7 years ago

I have an application in which I define some ExtensionPoints. I then want to load Implementations from these points to give functionality. When everything is in one file it works well as soon as I try to get them from a plugin directory it fails. with "name 'ITaskDAQ' is not defined.

Main file:

import pyutilib
import pyutilib.component.core

class ITaskDAQ(pyutilib.component.core.Interface):
    pass

class Application():
    observers = pyutilib.component.core.ExtensionPoint(ITaskDAQ)

    def __init__(self):
        pass

pyutilib.component.core.PluginGlobals.load_services(path='plugins', name_re='my.*')

Plugin file (my.plugin.py):

import pyutilib.component.core

class Worker(pyutilib.component.core.Plugin):
    pyutilib.component.core.implements(ITaskDAQ)
    pass

I am probably making a simple mistake, but I have gone through the documentation a few times and cannot find it.

geraldebberink commented 7 years ago

Found the solution by having yet another file which defines the interface. But be aware, in both the plugin and main file this should be imported in the same way.

import mypackage.api

and do not use in one file p.e.

import api

Mixing those does not work. Oh yeah and use an SingletonPlugin, if you want it to be created right away.