Functino
Functino

Reputation: 1935

"ImportError: No module named SocketServer" when trying to run Flask with Python 3

Flask has been installed with pip3 install flask. My code is as follows:

import flask
app = flask.Flask(__name__)

@app.route('/')
def hello():
    return "Hello World"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

When I run this code as Python 2, it produces the usual output and properly responds to requests. When run under Python 3, it produces no output and all attempts to connect to localhost:8080 or 127.0.0.1:8080 are refused.

When I kill the server, it gives me this message:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 58, in <module>
    from SocketServer import ThreadingMixIn, ForkingMixIn
ImportError: No module named 'SocketServer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "testflask.py", line 9, in <module>
    app.run(host='0.0.0.0', port=8080)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 758, in run
    from werkzeug.serving import run_simple
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 61, in <module>
    from socketserver import ThreadingMixIn, ForkingMixIn
  File "/home/samtheman/code/lasreader/rclick/socketserver.py", line 25, in <module>
    MyServer(s.accept()).start()
  File "/usr/lib/python3.4/socket.py", line 184, in accept
    fd, addr = self._accept()
KeyboardInterrupt
$ pip3 show flask werkzeug
---
Name: Flask
Version: 0.10.1
Location: /usr/local/lib/python3.4/dist-packages
Requires: Werkzeug, Jinja2, itsdangerous
---
Name: Werkzeug
Version: 0.10.4
Location: /usr/local/lib/python3.4/dist-packages
Requires: 
$ pip show flask werkzeug
---
Name: Flask
Version: 0.10.1
Location: /usr/local/lib/python2.7/dist-packages
Requires: Werkzeug, Jinja2, itsdangerous
---
Name: Werkzeug
Version: 0.9.6
Location: /usr/local/lib/python2.7/dist-packages
Requires: 

Upvotes: 1

Views: 8453

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122372

Your traceback reveals that the wrong module is being imported:

  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 61, in <module>
    from socketserver import ThreadingMixIn, ForkingMixIn
  File "/home/samtheman/code/lasreader/rclick/socketserver.py", line 25, in <module>
    MyServer(s.accept()).start()

See that second File line there? That's not the standard library socketserver, that's a different module altogether. As part of that module, it starts a socket server on import, so the Werkzeug import never completes and never gets to run properly.

Remove /home/samtheman/code/lasreader/rclick from your python path or remove that module altogether.

Upvotes: 3

Related Questions