yaml.constructor.ConstructorError: while constructing a Python object cannot find "module_name" in the module '__main__'
yaml.constructor.ConstructorError: while constructing a Python object cannot find "module_name" in the module '__main__'
我尝试从 file.yml
构建一些 classes
priters.yml的结构如下:
--- !Station
recipients:
- &first_phone ['Max']
- &second_phone ['Anna', 'Alisa']
obj:
- &first !!python/object:__main__.Nokia
model: Nokia_8800
recipients: *first_phone
- &second !!python/object:__main__.Apple
model: iPhone4
recipients: *second_phone
spam_station: !station
Nokia: *first
Apple: *second
class 构造函数出现在 spam_station.py
from abc import ABC, abstractmethod
from typing import Union
from yaml import YAMLObject, load
class AbstrackPhone(ABC):
@abstractmethod
def send_message(self, message):
pass
class Nokia(AbstrackPhone):
def __init__(self):
self.model = None
self.recipients = []
def send_message(self, message):
for recipient in self.recipients:
print(f"Hi {recipient} i'm {self.model}. {message}")
class Apple(AbstrackPhone):
def __init__(self):
self.model = None
self.recipients = []
def send_message(self, message):
for recipient in self.recipients:
print(f"Hi {recipient} i'm {self.model}. {message}")
class ConstructStation(YAMLObject):
yaml_tag = u'!Station'
@classmethod
def from_yaml(Class, loader, node):
def get_satation(loader, node):
data = loader.construct_mapping(node)
station = Class.Station()
station.add_phones(data.values())
return station
loader.add_constructor(u"!station", get_satation)
return loader.construct_mapping(node)['spam_station']
class Station():
def __init__(self):
self.senders = []
def add_phones(self, phones: Union[list, str]):
self.senders.extend(phones)
def send_message(self, message, **kwargs):
for sender in self.senders:
sender.send_message(message, **kwargs)
def station():
with open('../yaml_config/printers') as file:
spam_station = load(file)
return spam_station
if __name__ == "__main__":
station().send_message('Good luck!!!')
我尝试在 sender.py 中导入和使用 'station':
from station.spam_station import station
if __name__ == "__main__":
station().send_message('Good luck!!!')
当我运行spam_station.py,没问题:
Hi Max i'm Nokia_8800. Good luck!!!
Hi Anna i'm iPhone4. Good luck!!!
Hi Alisa i'm iPhone4. Good luck!!!
当我运行sender.py时,我有错误:
yaml.constructor.ConstructorError: while constructing a Python object
cannot find 'Nokia' in the module '__main__'
in "../yaml_config/printers", line 7, column 11
如何解决这个问题?请告诉我,将 python 对象配置为 yaml 的最佳做法是什么。谢谢!
问题是当您执行 sender.py
时,该文件(而不是 spam_station.py
)是 __main__
。
最好的解决方案可能是避免依赖于 YAML 文件中的导入路径。您已经使用 Station
执行此操作,因此您也可以简单地在其他 class 上执行此操作:
class Nokia(AbstrackPhone, YAMLObject):
yaml_tag = u"!Nokia"
def __init__(self, model = None, recipients = []):
self.model = model
self.recipients = recipients
def send_message(self, message):
for recipient in self.recipients:
print(f"Hi {recipient} i'm {self.model}. {message}")
现在您可以在 YAML 文件中使用 !Nokia
而不是 !!python/object:__main__.Nokia
。 Apple
class.
也是如此
我尝试从 file.yml
构建一些 classespriters.yml的结构如下:
--- !Station
recipients:
- &first_phone ['Max']
- &second_phone ['Anna', 'Alisa']
obj:
- &first !!python/object:__main__.Nokia
model: Nokia_8800
recipients: *first_phone
- &second !!python/object:__main__.Apple
model: iPhone4
recipients: *second_phone
spam_station: !station
Nokia: *first
Apple: *second
class 构造函数出现在 spam_station.py
from abc import ABC, abstractmethod
from typing import Union
from yaml import YAMLObject, load
class AbstrackPhone(ABC):
@abstractmethod
def send_message(self, message):
pass
class Nokia(AbstrackPhone):
def __init__(self):
self.model = None
self.recipients = []
def send_message(self, message):
for recipient in self.recipients:
print(f"Hi {recipient} i'm {self.model}. {message}")
class Apple(AbstrackPhone):
def __init__(self):
self.model = None
self.recipients = []
def send_message(self, message):
for recipient in self.recipients:
print(f"Hi {recipient} i'm {self.model}. {message}")
class ConstructStation(YAMLObject):
yaml_tag = u'!Station'
@classmethod
def from_yaml(Class, loader, node):
def get_satation(loader, node):
data = loader.construct_mapping(node)
station = Class.Station()
station.add_phones(data.values())
return station
loader.add_constructor(u"!station", get_satation)
return loader.construct_mapping(node)['spam_station']
class Station():
def __init__(self):
self.senders = []
def add_phones(self, phones: Union[list, str]):
self.senders.extend(phones)
def send_message(self, message, **kwargs):
for sender in self.senders:
sender.send_message(message, **kwargs)
def station():
with open('../yaml_config/printers') as file:
spam_station = load(file)
return spam_station
if __name__ == "__main__":
station().send_message('Good luck!!!')
我尝试在 sender.py 中导入和使用 'station':
from station.spam_station import station
if __name__ == "__main__":
station().send_message('Good luck!!!')
当我运行spam_station.py,没问题:
Hi Max i'm Nokia_8800. Good luck!!!
Hi Anna i'm iPhone4. Good luck!!!
Hi Alisa i'm iPhone4. Good luck!!!
当我运行sender.py时,我有错误:
yaml.constructor.ConstructorError: while constructing a Python object
cannot find 'Nokia' in the module '__main__'
in "../yaml_config/printers", line 7, column 11
如何解决这个问题?请告诉我,将 python 对象配置为 yaml 的最佳做法是什么。谢谢!
问题是当您执行 sender.py
时,该文件(而不是 spam_station.py
)是 __main__
。
最好的解决方案可能是避免依赖于 YAML 文件中的导入路径。您已经使用 Station
执行此操作,因此您也可以简单地在其他 class 上执行此操作:
class Nokia(AbstrackPhone, YAMLObject):
yaml_tag = u"!Nokia"
def __init__(self, model = None, recipients = []):
self.model = model
self.recipients = recipients
def send_message(self, message):
for recipient in self.recipients:
print(f"Hi {recipient} i'm {self.model}. {message}")
现在您可以在 YAML 文件中使用 !Nokia
而不是 !!python/object:__main__.Nokia
。 Apple
class.