pyYAML - 错误 - 属性错误:没有“load”属性
我在玩pyYAML这个库。我用Windows安装程序安装了适用于Python 2.7的版本。
它可以正常导入:
import yaml
而且没有报错。
但是,如果我这样做:
import yaml
f = open("sets.yml")
dataMap = yaml.load(f)
f.close()
print dataMap
就会出现一个属性错误,提示'module'对象没有'load'这个属性。
我试了dump,也得到了同样的错误。还有用这种方式导入时:
from yaml import load
也出现了同样的问题。
有人知道怎么回事吗?
哦,还有,我觉得很奇怪——每次我运行这个脚本,它都会生成一个.pyc文件。这是为什么呢?
3 个回答
1
@michaelfilms 的回答对我很有帮助。
我把我的测试文件命名为 yaml.py
,然后通过 import yaml
导入了 PyYAML,这样它就把自己当成了源文件。我把文件重命名为 test_yaml.py
。
我需要把导入的部分更新为 import yaml
,而不是自动更新的 import test_yaml
,现在一切都正常工作了。
1
PyYAML-3.10 版本中有一个叫做 load() 的功能:
jcomeau@intrepid:/usr/src/clusterFix$ easy_install pyyaml
Searching for pyyaml
Reading http://pypi.python.org/simple/pyyaml/
Reading http://pyyaml.org/wiki/PyYAML
Best match: PyYAML 3.10
Downloading http://pyyaml.org/download/pyyaml/PyYAML-3.10.zip
Processing PyYAML-3.10.zip
Running PyYAML-3.10/setup.py -q bdist_egg --dist-dir /tmp/easy_install-2PnFkZ/PyYAML-3.10/egg-dist-tmp-kCMq7S
build/temp.linux-i686-2.6/check_libyaml.c:2:18: fatal error: yaml.h: No such file or directory
compilation terminated.
libyaml is not found or a compiler error: forcing --without-libyaml
(if libyaml is installed correctly, you may need to
specify the option --include-dirs or uncomment and
modify the parameter include_dirs in setup.cfg)
zip_safe flag not set; analyzing archive contents...
Adding PyYAML 3.10 to easy-install.pth file
Installed /usr/local/lib/python2.6/dist-packages/PyYAML-3.10-py2.6-linux-i686.egg
Processing dependencies for pyyaml
Finished processing dependencies for pyyaml
jcomeau@intrepid:/usr/src/clusterFix$ python
Python 2.6.7 (r267:88850, Jun 13 2011, 22:03:32)
[GCC 4.6.1 20110608 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
>>> dir(yaml)
['AliasEvent', 'AliasToken', 'AnchorToken', 'BaseDumper', 'BaseLoader', 'BlockEndToken', 'BlockEntryToken', 'BlockMappingStartToken', 'BlockSequenceStartToken', 'CollectionEndEvent', 'CollectionNode', 'CollectionStartEvent', 'DirectiveToken', 'DocumentEndEvent', 'DocumentEndToken', 'DocumentStartEvent', 'DocumentStartToken', 'Dumper', 'Event', 'FlowEntryToken', 'FlowMappingEndToken', 'FlowMappingStartToken', 'FlowSequenceEndToken', 'FlowSequenceStartToken', 'KeyToken', 'Loader', 'MappingEndEvent', 'MappingNode', 'MappingStartEvent', 'Mark', 'MarkedYAMLError', 'Node', 'NodeEvent', 'SafeDumper', 'SafeLoader', 'ScalarEvent', 'ScalarNode', 'ScalarToken', 'SequenceEndEvent', 'SequenceNode', 'SequenceStartEvent', 'StreamEndEvent', 'StreamEndToken', 'StreamStartEvent', 'StreamStartToken', 'TagToken', 'Token', 'ValueToken', 'YAMLError', 'YAMLObject', 'YAMLObjectMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '__with_libyaml__', 'add_constructor', 'add_implicit_resolver', 'add_multi_constructor', 'add_multi_representer', 'add_path_resolver', 'add_representer', 'compose', 'compose_all', 'composer', 'constructor', 'dump', 'dump_all', 'dumper', 'emit', 'emitter', 'error', 'events', 'load', 'load_all', 'loader', 'nodes', 'parse', 'parser', 'reader', 'representer', 'resolver', 'safe_dump', 'safe_dump_all', 'safe_load', 'safe_load_all', 'scan', 'scanner', 'serialize', 'serialize_all', 'serializer', 'tokens']
20
如果你的系统路径中有一个叫做 yaml.py 的文件,并且这个文件在实际的 PyYaml 库之前,那么你导入的就是这个 yaml.py 文件。也就是说,如果你自己创建的文件也叫 yaml.py,就会出现这个问题。
你在目录中看到 yaml.pyc 文件,说明你正好遇到了这个情况。你的 import yaml 语句正在加载你自己写的 yaml.py 文件,这导致解释器把它编译成 yaml.pyc,以便更高效地运行。
建议你把目录中的 yaml.py 文件重命名。一般来说,不要把你正在编写的 Python 文件命名为任何已经存在的 Python 模块的名字。