Forums

ModuleNotFoundError: No module named 'SocketServer'

I'm trying to work through part of the Flask tutorial (https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-email-support) to add email support for my Flask app. However, after adding the code, I get an error whenever I try to load any page on my Flask. The error log shows:

2019-12-31 04:34:25,908: Error running WSGI application
2019-12-31 04:34:25,910: ModuleNotFoundError: No module named 'SocketServer'
2019-12-31 04:34:25,910:   File "/var/www/jplank_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-12-31 04:34:25,910:     from flask_app import app as application  # noqa
2019-12-31 04:34:25,910: 
2019-12-31 04:34:25,910:   File "/home/jplank/mysite/flask_app.py", line 2, in <module>
2019-12-31 04:34:25,910:     from flask_sqlalchemy import SQLAlchemy
2019-12-31 04:34:25,910: 
2019-12-31 04:34:25,911:   File "/usr/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 14, in <module>
2019-12-31 04:34:25,911:     from flask import _app_ctx_stack, abort, current_app, request
2019-12-31 04:34:25,911: 
2019-12-31 04:34:25,911:   File "/usr/lib/python3.8/site-packages/flask/__init__.py", line 16, in <module>
2019-12-31 04:34:25,911:     from werkzeug.exceptions import abort
2019-12-31 04:34:25,911: 
2019-12-31 04:34:25,911:   File "/usr/lib/python3.8/site-packages/werkzeug/__init__.py", line 218, in <module>
2019-12-31 04:34:25,912:     from .serving import run_simple
2019-12-31 04:34:25,912: 
2019-12-31 04:34:25,912:   File "/usr/lib/python3.8/site-packages/werkzeug/serving.py", line 59, in <module>
2019-12-31 04:34:25,912:     import SocketServer as socketserver
2019-12-31 04:34:25,912: ***************************************************
2019-12-31 04:34:25,912: If you're seeing an import error and don't know why,
2019-12-31 04:34:25,912: we have a dedicated help page to help you debug: 
2019-12-31 04:34:25,913: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-12-31 04:34:25,913: ***************************************************

Can anyone help me understand what might be causing the error? My googling shows that there was once an error with werkzeug with the line " import SocketServer as socketserver" but that it's since been fixed. The problem seemed to be that socketserver should be lowercase for python 3+. And when I look at the code in serving.py, it shows:

try:
    import socketserver
    from http.server import BaseHTTPRequestHandler
    from http.server import HTTPServer
except ImportError:
    import SocketServer as socketserver
    from BaseHTTPServer import HTTPServer
    from BaseHTTPServer import BaseHTTPRequestHandler

So, it does actually look like it ought to be using the "import socketserver" line. I'm just really confused what's going on here. Can anyone help?

That's certainly very odd! What happens if you start a Python 3.8 console from the "Consoles" page, then run

import socketserver
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer

...?

Ok, I entered those into a python 3.8 console and there were no errors! Weird!! Any idea what might be happening?

That is indeed very weird! You don't happen to have any files called socketserver.py or http.py, or directories called socketserver or http, do you?

No, I definitely do not.

Could you start a bash console, then run

python3.8 -i /var/www/jplank_pythonanywhere_com_wsgi.py

...and try the imports there?

Here's what shows up when I enter that line into bash:

18:55 ~ $ python3.8 -i /var/www/jplank_pythonanywhere_com_wsgi.py
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/werkzeug/serving.py", line 56, in <module>
    from http.server import BaseHTTPRequestHandler
  File "/usr/lib/python3.8/http/server.py", line 92, in <module>
    import email.utils
  File "/home/jplank/mysite/email.py", line 1, in <module>
    from flask_mail import Message
ModuleNotFoundError: No module named 'flask_mail'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/var/www/jplank_pythonanywhere_com_wsgi.py", line 16, in <module>
    from flask_app import app as application  # noqa
  File "/home/jplank/mysite/flask_app.py", line 2, in <module>
    from flask_sqlalchemy import SQLAlchemy
  File "/usr/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 14, in <module>
    from flask import _app_ctx_stack, abort, current_app, request
  File "/usr/lib/python3.8/site-packages/flask/__init__.py", line 16, in <module>
    from werkzeug.exceptions import abort
  File "/usr/lib/python3.8/site-packages/werkzeug/__init__.py", line 218, in <module>
    from .serving import run_simple
  File "/usr/lib/python3.8/site-packages/werkzeug/serving.py", line 59, in <module>
    import SocketServer as socketserver
ModuleNotFoundError: No module named 'SocketServer'

I think that I've figured it out. Problem seemed to stem from my having a file names email.py. Thank you for all the help troubleshooting this!

ah that sounds likely- that file might be imported instead of the actual python email library