[英]python subclass can't import superclass from superclass
First of all is very possible I can making a terrible mistake. 首先,我很可能犯一个可怕的错误。 But let's go! 但是,走吧!
My superclass (project/src/mlbase.py) 我的超类(project / src / mlbase.py)
from preprocessing import PreProcessing
class MLBase:
preProcessing = None
def __init__(self,preprocessingOptions):
self.preProcessing = PreProcessing(preProcessingOptions)
# and more stuff here...
My Subclass(project/src/preprocessing.py) 我的子类(project / src / preprocessing.py)
from mlbase import MLBase
class PreProcessing(MLBase):
def __init__(self,options):
#processing options here...
pass
My script that is instantiating everything(project/main.py) 我的脚本实例化了一切(project / main.py)
from src.mlbase import MLBase
mlb = MLBase(preProcessingOptions = {})
Dirs DIRS
"""
project
|
+ src
|
+ mlbase.py
|
+ preprocessing.py
|
+ main.py
"""
As you can see. 如你看到的。 The objective is instanciate subclasses from superclass. 目的是要实例化超类的子类。 But I receive the following error when src/preprocessing.py
module tries to import MLBase
class from src.mlbase.py
: 但是当src/preprocessing.py
模块尝试从src.mlbase.py
导入MLBase
类时,我收到以下错误:
ImportError: cannot import name MLBase ImportError:无法导入名称MLBase
Why this is happening? 为什么会这样呢?
It's just a little typo. 这只是一个错字。 You declared class MBase
but tried to import MLBase
. 您声明了class MBase
但尝试导入MLBase
。 All you have to do is change the superclass file to this: 您所要做的就是将超类文件更改为此:
from preprocessing import PreProcessing
class MLBase: #Note that it's "MLBase", not "MBase"
preProcessing = None
def __init__(self,preprocessingOptions):
self.preProcessing = PreProcessing(preProcessingOptions)
# and more stuff here...
The solution was import PreProcessing class using from preprocessing import PreProcessing
inside the constructor method! 解决方案是在构造函数方法中使用from preprocessing import PreProcessing
导入Import PreProcessing类! I don't know why! 我不知道为什么! I really would like to understand that! 我真的很想了解!
In mlbase module: 在mlbase模块中:
class MLBase:
def __init__(self,preProcessingOptions):
from preprocessing import Preprocessing
# more stuff
In preprocessing module 在预处理模块中
from mlbase import MLBase
class PreProcessing(MLBase):
def __init__(self,preProcessingOptions):
# more stuff
Too weird for me! 对我来说太奇怪了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.