安装:Python3.3
我正在制作一个应用程序,它在名为“sources”的文件夹中查找.py文件,并在其中查找扩展我定义的名为“SourceBase”的类的类。如果它们扩展了SourceBase,我想创建一个新的类实例来处理。在
我通过以下帖子做了一些相当数量的研究,我基本上都理解:
我的文件夹设置是这样的,我认为这是相关的:
EPDownloader [package]
\
epdownloader.py [main]
SourceBase.py [contains SourceBase class]
imageutils.py [this class will find and dynamically load the classes in the sources package]
sources [package]
\
source1.py [has class that extends SourceBase]
source2.py
...other plugins here...
我的问题是我使用了以下代码(来自我上面列出的其他堆栈溢出问题),它在我的模块中搜索类,但是它找不到我的类。它只是跳过它们。我不知道怎么了。以下是我执行搜索的代码(基于我发布的第一个链接):
^{pr2}$下面是它给我的相关输出(我删减了代码输出的许多其他内容):
Inspecting item from module: source1
Get attribute: <module 'sources.source1' from '/Users/Mgamerz/Documents/workspace/code/EPDownloader/sources/source1.py'>
[] <--- signifies it failed to find the source class
我很确定我的子类化可以工作,下面是类的一个片段:
from EPDownloader import SourceBase
class source1(SourceBase.SourceBase):
def __init__(self):
pass
我被这个问题难住了。我在这上面花了几个小时,不知道该怎么办。我觉得这是一个我没有看到的简单的解决办法。有人能帮我找到这里的窃听器吗?在
[注意:我查看了StackOverflow格式化帮助,但没有找到任何格式化“highlight”的方法,即在文本上放置灰色背景,而是内联的。这将有助于突出我要传达的问题的部分内容。]
您的
__import__
有问题:没有导入模块, 您正在导入整个包(整个“sources”目录作为一个包)。在我可以修改你的代码:
查看文档:http://docs.python.org/3.1/library/functions.html#import
只需更换
与
^{pr2}$和“从”一样源.source1import*”告诉
__import__
获取给定模块内的所有内容。在相关问题 更多 >
编程相关推荐