Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyYAML - Error - Attribute Error: no attribute "load"

Tags:

python

pyyaml

Well, I'm playing with pyYAML. I installed the version for Python 2.7 with the Windows installer.

It imports just fine:

import yaml

and it throws no errors.

But, if I do this:

import yaml

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

print dataMap

It throws an attribute error and says 'module' object has no attribute 'load'.

I tried dump, and got the same thing. Also same thing for importing like this:

from yaml import load

Anyone have any ideas?

Oh, and also, I thought it was weird -- whenever I run the script, it created a .pyc of it. Why is that?

like image 529
adamjseitz Avatar asked Jun 07 '26 07:06

adamjseitz


1 Answers

If another file named yaml.py is on your sys.path BEFORE the actual PyYaml library, you'll pick up and import that yaml.py file. This includes if you've named your own file yaml.py.

The fact that you're getting yaml.pyc in your directory indicates that this is exactly what you're doing. Your import yaml statement is loading in your own yaml.py file, which causes the interpreter to compile it to yaml.pyc for more efficient running.

Rename the yaml.py file in your directory. Do not name any python file you're working on with the same name as any existing python module you're using, as a general rule.

like image 122
michaelfilms Avatar answered Jun 08 '26 21:06

michaelfilms