can get attribute 'my_function' on <module '__main__' (built-in)> erro in Flask with apache2 in aws

1,360 views
Skip to first unread message

Edwin Rueda

unread,
Oct 18, 2020, 5:37:58 PM10/18/20
to modwsgi
Hello everyone, I am having an error when I load a model saved with pickle, the error is as follows:

[Sun oct 18 12:35:54.049848 2020] [wsgi:error] [pid 2775:tid 139848716216064] [client 179.67.95.54:53634] AttributeError: Can't get attribute 'word_normalization' on <module '__main__' (built-in)>, referer http://ec2-54-232-139-27.sa.compute.amazonaws.com/

the problem is that the pickle object that I load depends on a function called 'word_normalization', locally I call the function first before loading the pickle object and it works, but already on the server I load the function first (as shown in the image below) and it generates the aforementioned error. I would like to know how to solve this problem, thank you.

Selección_087.png

Graham Dumpleton

unread,
Oct 18, 2020, 5:45:18 PM10/18/20
to mod...@googlegroups.com
Move any code you need for pickled objects out of your main Python code file and put it in a separate module, that you import and use, with pickling done from the context of the imported module.

This is necessary because under mod_wsgi there is no __main__ module, so pickled functions/object types can only come from separate imported modules.

Issues with pickle are covered in the documentation at:


Graham

<Selección_087.png>

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/27c0e972-c6a7-48b8-95e5-26d651e3d817n%40googlegroups.com.
<Selección_087.png>

Edwin Rueda

unread,
Oct 30, 2020, 9:48:03 AM10/30/20
to mod...@googlegroups.com
Hi Graham, thanks for answering, I tried what you told me, I save in a file called "utilities.py" the function that my pickle object needs,  and then I import it as seen in the following image:
Selección_100.png
Later, in the app.py file I import the module that contains the "word_normalization" function and load the pickle object, even so, the error continues, it tells me that it cannot find the "utilities" module, I attach the images of my path project where I save the module.
Selección_099.png
Selección_098.png



--
Edwin J. Rueda
Maestrando en Ciencias de la Computación
Universidade Federal do Pará, Brasil
Ingeniero de Sistemas
Universidad Industrial de Santander, Colombia

Edwin Rueda

unread,
Oct 30, 2020, 10:03:22 AM10/30/20
to mod...@googlegroups.com
I already solved it, you have to create strictly the same path to load the model, when I trained the model that then saved as a pickle object, I loaded the module using "from utilities import word_normalization", and once on the server I loaded the module from "from static.code.utilities import word_normalization", that's where the error was, I had to create locally that same directory scheme locally to be able to load the module using "from static.code.utilities import word_normalization". Thank you very much for your help!
Reply all
Reply to author
Forward
0 new messages