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
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.
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.
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).
精彩评论