2012-02-15 10 views
0

まあ、私はpyYAMLで遊んでいます。私はPython 2.7用のバージョンをWindowsインストーラでインストールしました。pyYAML - エラー - 属性エラー:no attribute "load"

それはうまくインポート:

import yaml 

、それはエラーをスローしません。

しかし、私はこれを行う場合:

それは属性のエラーをスローし、「モジュール」オブジェクトが属性「負荷」を持っていないと言う
import yaml 

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

print dataMap 

私はダンプを試みて、同じことを得ました。このようにインポートする場合も同じです:

from yaml import load 

誰でもアイデアはありますか?

ああ、私はそれが変わったと思った。私がスクリプトを実行するたびに、それは.pycを作成した。何故ですか?

+0

'.pyc'については、http://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files – charlax

+0

をご覧ください.YAMLについては、どのライブラリを使用していますか?あなたのフォルダに 'yaml.py'ファイルがありますか? – charlax

答えて

6

、あなたがピックアップしたファイルをインポートyaml.pyます。これはあなた自身のファイルyaml.pyの名前を付けた場合を含みます。

ディレクトリにyaml.pycが表示されているということは、まさにあなたがやっていることを示しています。あなたのimport yamlステートメントがあなた自身のyaml.pyファイルにロードされています。これにより、インタープリタはyaml.pycにコンパイルしてより効率的に実行できます。

ディレクトリ内のyaml.pyファイルの名前を変更してください。一般的なルールとして、使用している既存のPythonモジュールと同じ名前のPythonファイルに名前を付けないでください。

+1

ああ、ありがとう。私は前にこれをやった。私はそれが "test.py"という名前にしたくないからだと思います。なぜなら、私のデスクトップは、新しいモジュールをテストするときに、それらの行に沿ったものが混乱するからです。 うまくいけば、私はこれを覚えています。 – musketeer925

0

PyYAMLと-3.10)(負荷を持っていますyaml.pyという名前の別のファイルには、実際のP​​yYAMLとライブラリのsys.path前に、あなたの上にある場合は

[email protected]:/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 
[email protected]:/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'] 
関連する問題