Running Flask dev server in Python 3.6 raises ImportError for SocketServer and ForkingMixIn

Question:

I am trying to run a basic Flask app using Python 3.6. However, I get an ImportError: cannot import name 'ForkingMixIn'. I don’t get this error when running with Python 2.7 or 3.5. How can I run Flask with Python 3.6?

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"
Traceback (most recent call last):
  File "C:Python36libsite-packageswerkzeugserving.py", line 65, 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 ".fsk.py", line 9, in <module>
    app.run()
  File "C:Python36libsite-packagesflaskapp.py", line 828, in run
    from werkzeug.serving import run_simple
  File "C:Python36libsite-packageswerkzeugserving.py", line 68, in <module>
    from socketserver import ThreadingMixIn, ForkingMixIn
ImportError: cannot import name 'ForkingMixIn'
Asked By: SharpCoder

||

Answers:

This is fixed as of Werkzeug 0.11.15. Make sure you have installed the latest version of Werkzeug. pip install -U werkzeug.


This is a known issue that was reported to Werkzeug in anticipation of Python 3.6. Until that or another patch is merged and released, Werkzeug’s dev server will not run on Python 3.6.

Check if OS can fork before importing ForkingMixIn since Python 3.6 will no longer define that when it is not available on the operating system (python/cpython@aadff9b) and ImportError: cannot import name 'ForkingMixIn' will occur.

In the mean time, you can run your app with an external WSGI server such as Gunicorn.

pip install gunicorn
gunicorn my_app:app

You can wrap your app in the debug middleware if you need the in-page debugger (as long as you only run Gunicorn with one worker).

Answered By: davidism
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.