0

firstly dir_names works well when pickling

dir_names = collections.namedtuple('dir_names', ['mark', 'category'])
pickle.dump(dir_names, open('tmp.bin', 'wb'))

I want make dir_name a const

DIR_NAMES = collections.namedtuple('dir_names', ['mark', 'category'])
pickle.dump(DIR_NAMES, open('tmp.bin', 'wb'))

but when pickling it, i got this error

Traceback (most recent call last):
  File "/Users/zyq/CC_Cat/warc_process.py", line 376, in <module>
    pickle.dump(DIR_NAMES, open('tmp.bin', 'wb'))
_pickle.PicklingError: Can't pickle <class '__main__.dir_names'>: attribute lookup dir_names on __main__ failed

I wonder why it's goes bad when being a const, and why DIR_NAMES been lowerwised at __main__.dir_names

I tried defaultdict works well

DD = collections.defaultdict(int)
pickle.dump(DD, open('tmp.bin', 'wb'))
5
  • always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback.
    – furas
    Commented Dec 11, 2023 at 5:40
  • using uppercase name DIR_NAMES shouldn't create constant
    – furas
    Commented Dec 11, 2023 at 5:41
  • it seems it needs the same name in namedtuple("DIR_NAMES", ...) as variable DIR_NAMES = ...
    – furas
    Commented Dec 11, 2023 at 5:47
  • but why DIR_NAMES been lowercased at traceback, got me confused Commented Dec 11, 2023 at 5:52
  • traceback shows name from namedtuple('dir_names'
    – furas
    Commented Dec 11, 2023 at 5:54

1 Answer 1

0

It seems pickle needs eexactly the same name in namedtuple("DIR_NAMES", ...) as in variable DIR_NAMES = ...

DIR_NAMES = collections.namedtuple('DIR_NAMES', ['mark', 'category'])
pickle.dump(DIR_NAMES, open('tmp.bin', 'wb'))

You can see this problem also in answers:

1
  • got it, thx🙂, not even thinking of it Commented Dec 11, 2023 at 5:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.