views:

98

answers:

3

Here is the code I have:

import pickle 

alist = ['here', 'there']
c = open('config.pck', 'w')

pickle.dump(alist, c)

and this is the error I receive:

Traceback (most recent call last):
  File "C:\pickle.py", line 1, in ?
import pickle
  File "C:\pickle.py", line 6, in ?
pickle.dump(alist, c)
AttributeError: 'module' object has no attribute 'dump'

whats going on? I am using python 2.4 on windows xp

+5  A: 

Don't call your file pickle.py. It conflicts with the python standard libary module of the same name. So your import pickle is not picking up the python module.

unutbu
beat me to it :) I'll delete mine. but shouldn't this throw some sort of circular reference error? infinite loop? ;)
froadie
Thanks, that did the job. Now that you said that I remember reading about this issue in the past. I will remember not to name my scripts the same as modules that I import :D
Richard
@froadie: Hehe, actually, I don't think so, because `import module` does not reload the module if it has already been imported.
unutbu
+1  A: 

The code you have works fine for me.

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>>
>>> alist = ['here', 'there']
>>> c = open('config.pck', 'w')
>>>
>>> pickle.dump(alist, c)
>>>

The issue is that your filename "pickle.py" is making the import pickle statement try to import from your own file instead of the main library. Rename your code file.

Amber
+1  A: 

Your script is called pickle and therefore shadows the module picke from the standard library. It imports itself and tries to call its dump function (and of course it doesn't have one).

Note that you're "lucky" that you don't get kicked into an infinite import loop (because importing the same module twice just creates another reference to the same module object in memory).

delnan