2012-10-09 3 views
0

PyYAML을 사용하여 YAML 파일을 덤프하고로드하는 데 문제가 있습니다.PyYAML 덤프 오브젝트 경로

두 개의 분리 된 응용 프로그램 A와 B가 있습니다. YAML 파일을 A에 덤프하고 나중에로드하여 B에서 사용하고 싶습니다. 그러나 개체 경로가 올바르지 않은 것 같습니다. dump.py에서

A-folder 
    dump.py 
B-folder 
    the_module.py 
    use.py 

, 나는이 같은 코드 : 나는 use.py.이 YAML 파일을 사용할 필요가 그런

!!python/object:B-folder.the_module.the_class 
attribute_0: !!python/long '10' 
attribute_1: !!python/long '10' 

: 그것은 YAML 파일을 제공

yaml.dump(the_class_instance, file_stream, default_flow_style=False) 

하지만 the_module.the_module.the_class의 인스턴스로 제대로로드 할 수 없습니다. 그것은 말한다 :

cannot find module 'B-folder.the_module' (No module named B-folder.the_module) 

내가 그냥 B-folder.adaptor의 메소드를 호출하지만 여전히 같은 결과를 제공 dump.py에서 다른 모듈의 B-folder.adaptor에 덤핑을하려고 노력했다.

어떻게 처리하나요? 감사.

답변

1

여기의 문제는 실제로 PyYAML과 관련이 없으며, Python 모듈 로딩과 관련이 있습니다.

A에서 나는 the_module을 import B-folder.the_module 또는 from B-folder import the_module으로 B 폴더 패키지의 일부로 가져 오는 것으로 가정합니다. 이 경우 모듈 이름은 B-folder.the_module입니다. 이 파일은 YAML 파일에 저장됩니다.

B에서 저는 import the_module과 같이 내부적으로 the_module을 가져오고 있다고 가정합니다. 이 경우 모듈 이름은 the_module입니다. 이는 B-folder.the_module과 같지 않으므로 오류가 발생합니다. from B-folder import the_module 또는 import B-folder.the_module을 사용하여 B로 가져온 경우 동일한 폴더에 있어도 문제가 해결됩니다.

관련 문제