1

I have searched around but unfortunately found nothing that could help me to solve this problem. I have made this unigram tagger and have saved it in this way :

output = open("nameofthefile.pkl","wb")
pickle.dump(taggerfunction,output)
output.close()

45B is saved into the "nameofthefile.pkl" so it is not empty.

then I close the jupyter notebook and open another one and try to load the pickle file in this way:

inp = open("nameofthefile.pkl","rb")
tagger = pickle.load(inp)
inp.close()

but I get this error :

AttributeError: Can't get attribute 'nameofthefunction' on <module 'main'>


Somebody said that it is because I have imported a module in the function that I saved for pickling and when I try to pickle load the function, it does not work.

but I built another module to test whether the error was because of that:

def another_function(numb1,numb2):
    if isinstance(numb1 and numb2,int):
        return numb1+numb2
    else:
        return "your values are not all INTEGERS"

then I pickled it:

output = open("justafunction.pkl","wb")
pickle.dump(another_function,output)
output.close()

I built this in a jupyter notebook called, let's say "notebook_1" and then went and open another notebook, let's say it's called "notebook_2" and tried to pickle load the function:

inp = open("justafunction.pkl","rb")
pickle.load(inp)
inp.close()

but still got the same error:

------------------------------------------------- 
--------------------------
AttributeError                            
Traceback (most recent call last)
<ipython-input-2-416ed412eb60> in <module>
1 inp = open("justafunction.pkl","rb")
 ----> 2 pickle.load(inp)

AttributeError: Can't get attribute 
'another_function' on <module '__main__'>

IS IT BECAUSE I USE DIFFERENT NOTEBOOKS FOR DUMPING AND LOADING THE PICKLE FILE??

csismylife
  • 71
  • 10
  • 1
    There is a `nameofthefunction` reference in your first notebook that is not resolved - probably it's a function that you import from another module, rather than one defined in the notebook itself -, so the second notebook cannot access it, hence the `AttributeError` – crissal Apr 24 '22 at 06:30
  • yes, in the function I have used some nltk library modules, but even when I make a simple function without referencing anything, I still get the same error. – csismylife Apr 24 '22 at 07:09
  • 1
    Full traceback please. We/you need to see whether the error occurs in the `open` line or the `pickle` one. – hpaulj Apr 24 '22 at 14:46
  • 1
    And when you say you use different notebooks, are they running on the same system in the same environment or is something different? – Wayne Apr 24 '22 at 19:11
  • @hpaulj thanks. I edited the question. the open() does not show any error, it's the pickle.load(). – csismylife Apr 25 '22 at 07:59
  • @Wayne yes. the same system and the same environment. even if I come out of the notebook and re-enter it again, the pickle does not work. – csismylife Apr 25 '22 at 08:01
  • Have you looked at [here](https://stackoverflow.com/a/64579104/8508004) and [here](https://stackoverflow.com/a/1253895/8508004) and tried some of the functions that should work in modern pickle in the answer by am70? If even some of the examples that should work, don't you could probably search the issues; it looks like 'cpython' is where pickle goes based on [this example](https://github.com/python/cpython/issues/91299), but I'm not 100% sure. Also have you tried Dill? – Wayne Apr 25 '22 at 14:52
  • I don't know if it does any better with what you are trying to do. Also, less important to try know about, but if you do this stuff in a script does it work? (There I'm getting at that at least you can cross Jupyter off being involved? You currently have it tagged as a topic.) – Wayne Apr 25 '22 at 14:52
  • Also, it looks like the underlying cause of the quoted gotcha / issue and proposed solution in the third paragraph [in this answer here](https://stackoverflow.com/questions/70351690/is-it-possible-to-somehow-save-a-python-object-to-a-dataframe-as-a-column-value#) is what you were trying to address with your `justafunction.pkl`. Having another example with sources cited may help someone else. Unless you look at the link and see you didn't fully try that yet? – Wayne Apr 25 '22 at 15:16

0 Answers0