Reputation: 9560
I have a problem in dumping and loading the YAML files using PyYAML.
I have two separated applications A and B. I would like to dump a YAML file in A, and later load it and use it in B. But the path of the objects seems incorrect.
A-folder
dump.py
B-folder
the_module.py
use.py
In dump.py, I have code like:
yaml.dump(the_class_instance, file_stream, default_flow_style=False)
It gives a YAML file:
!!python/object:B-folder.the_module.the_class
attribute_0: !!python/long '10'
attribute_1: !!python/long '10'
Then I need to use this YAML file in use.py. But I cannot load it correctly as an instance of the_module.the_module.the_class. It says:
cannot find module 'B-folder.the_module' (No module named B-folder.the_module)
I tried to do the dumping in another module B-folder.adaptor, in dump.py it just calls the methods in B-folder.adaptor, but it still gives the same result.
How to deal with it? Thanks.
Upvotes: 0
Views: 839
Reputation: 9888
The problem here isn't actually with PyYAML, it's with Python's module loading.
In A, I'm assuming that you're importing the_module as part of the B-folder package, either with import B-folder.the_module
or from B-folder import the_module
. In this case, the module name is B-folder.the_module
. This gets put into the YAML file, as you can see.
In B, I'm assuming that you're just importing the_module internally, with something like import the_module
. In this case, the module name is the_module
. That's not the same as B-folder.the_module
, which is why you get the error. If you instead imported in B using from B-folder import the_module
or import B-folder.the_module
, even though you're in the same folder, it should solve the problem.
Upvotes: 1