pyYAML - 错误 - 属性错误:没有属性“加载”

时间:2012-02-15 01:16:52

标签: python pyyaml

好吧,我正在玩pyYAML。我使用Windows安装程序安装了Python 2.7的版本。

导入得很好:

import yaml

并且没有错误。

但是,如果我这样做:

import yaml

f = open("sets.yml")
dataMap = yaml.load(f)
f.close()

print dataMap

它抛出一个属性错误,并说'module'对象没有属性'load'。

我尝试了转储,并得到了相同的东西。导入的内容也是如此:

from yaml import load

有人有什么想法吗?

哦,而且,我认为这很奇怪 - 每当我运行脚本时,它就会创建一个.pyc。那是为什么?

2 个答案:

答案 0 :(得分:8)

如果另一个名为yaml.py的文件位于sys.path之前,则在实际的PyYaml库之前,您将选择并导入该yaml.py文件。这包括您是否已将自己的文件命名为yaml.py。

您在目录中获取yaml.pyc的事实表明这正是您正在做的事情。您的import yaml语句正在您自己的yaml.py文件中加载,这会导致解释器将其编译为yaml.pyc以便更有效地运行。

重命名目录中的yaml.py文件。作为一般规则,不要将您正在使用的任何python文件命名为与您正在使用的任何现有python模块相同的名称。

答案 1 :(得分:0)

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']