You meant "for packages there is the approach...". Actually, that works for every module (__init__.py
is a module, just with special semantics). Use __all__
inside the plugin modules and that's it.
But remember: __all__
only limits what you import using from xxxx import *
; you can still access the rest of the module, and there's no way to avoid that using the standard Python import mechanism.
If you're using some kind of active introspection technique (eg. exploring the namespace in the module and then importing classes from it), you could check if the class comes from the same file as the module itself.
You could also implement your own import mechanism (using importlib
, for example), but that may be overkill...
Edit: for the "check if the class come from the same module":
Say that I have two modules, mod1.py
:
class A(object):
pass
and mod2.py
:
from mod1 import A
class B(object):
pass
Now, if I do:
from mod2 import *
I've imported both A
and B
. But...
>>> A
<class 'mod1.A'>
>>> B
<class 'mod2.B'>
as you see, the classes carry information about where did they originate. And actually you can check it right away:
>>> A.__module__
'mod1'
>>> B.__module__
'mod2'
Using that information you can discriminate them easily.