[英]Python __subclasses__() not listing subclasses
I cant seem to list all derived classes using the __subclasses__()
method. 我似乎无法使用
__subclasses__()
方法列出所有派生类。 Here's my directory layout: 这是我的目录布局:
import.py
backends
__init__.py
--digger
__init__.py
base.py
test.py
--plugins
plugina_plugin.py
From import.py
i'm calling test.py
. 从
import.py
我正在调用test.py
test.py
in turn iterates over all the files in the plugins
directory and loads all of them. test.py
依次遍历plugins
目录中的所有文件并加载所有文件。 test.py
looks like this: test.py
看起来像这样:
import os
import sys
import re
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ )))))
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ ))), 'plugins'))
from base import BasePlugin
class TestImport:
def __init__(self):
print 'heeeeello'
PLUGIN_DIRECTORY = os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ ))), 'plugins')
for filename in os.listdir (PLUGIN_DIRECTORY):
# Ignore subfolders
if os.path.isdir (os.path.join(PLUGIN_DIRECTORY, filename)):
continue
else:
if re.match(r".*?_plugin\.py$", filename):
print ('Initialising plugin : ' + filename)
__import__(re.sub(r".py", r"", filename))
print ('Plugin system initialized')
print BasePlugin.__subclasses__()
The problem us that the __subclasses__()
method doesn't show any derived classes. 我们的问题是
__subclasses__()
方法没有显示任何派生类。 All plugins in the plugins
directory derive from a base class in the base.py
file. plugins
目录中的所有插件都派生自base.py
文件中的基类。
base.py
looks like this: base.py
看起来像这样:
class BasePlugin(object):
"""
Base
"""
def __init__(self):
pass
plugina_plugin.py
looks like this: plugina_plugin.py
看起来像这样:
from base import BasePlugin 从基地导入BasePlugin
class PluginA(BasePlugin):
"""
Plugin A
"""
def __init__(self):
pass
Could anyone help me out with this? 任何人都可以帮我解决这个问题吗? Whatm am i doing wrong?
我做错了什么? I've racked my brains over this but I cant seem to figure it out
我绞尽脑汁,但我似乎无法弄明白
Thanks. 谢谢。
There were no other base.py files. 没有其他base.py文件。 I'm on a WinXP (SP2) with Python 2.6.
我使用的是Python 2.6的WinXP(SP2)。 I added another class to my
test.py
file called PluginB
which used BasePlugin
as the base class. 我在我的
test.py
文件中添加了另一个类,该文件名为PluginB
,它使用BasePlugin
作为基类。 When i did 当我做的时候
print PluginA.__mro__
print PluginB.__mro__
I got: 我有:
(<class 'plugina_plugin.PluginA'>, <class 'base.BasePlugin'>, <type 'object'>)
(<class 'backends.digger.test.PluginB'>, <class 'backends.digger.base.BasePlugin'>, <type 'object'>)
As you can see, they're both using the same base plugin but the qualified names are different. 如您所见,它们都使用相同的基本插件,但限定名称不同。 This was because in
plugina_plugin.py
I was importing BasePlugin
like this: 这是因为我在
plugina_plugin.py
中导入了BasePlugin
如下所示:
from base import BasePlugin
Instead of: 代替:
from backends.digger.base import BasePlugin
Fixing this fixed it. 修复它修复它。
Perhaps the plugins are finding another base.py
hiding somewhere (the plugin directory for example). 也许插件正在寻找隐藏在某处的另一个
base.py
(例如插件目录)。
Run with python -v
to see if two different base.py
are getting imported 运行
python -v
以查看是否导入了两个不同的base.py
You can also look at PluginA.__mro__
and confirm that the BasePlugin
in there is the right one 您还可以查看
PluginA.__mro__
并确认其中的BasePlugin
是正确的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.