简体   繁体   English

Python __subclasses __()没有列出子类

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM