Werkzeug: ImportError causing Exception while handling an Exception

Created on 22 Dec 2016  ·  5Comments  ·  Source: pallets/werkzeug

After a bit of debugging, I have narrowed the following error down to werkzeug/http.py#L26, which gives the following exception: ImportError: cannot import name 'parse_http_list'. Below is the entire traceback, which involves an exception inside the exception attempting to fallback to an old python reference.

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/werkzeug/http.py", line 26, in <module>
    from urllib2 import parse_http_list as _parse_list_header
ImportError: No module named 'urllib2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "examples/http.py", line 2, in <module>
    from flask import Flask
  File "/usr/local/lib/python3.5/site-packages/flask/__init__.py", line 17, in <module>
    from werkzeug.exceptions import abort
  File "/usr/local/lib/python3.5/site-packages/werkzeug/__init__.py", line 152, in <module>
    __import__('werkzeug.exceptions')
  File "/usr/local/lib/python3.5/site-packages/werkzeug/exceptions.py", line 71, in <module>
    from werkzeug.wrappers import Response
  File "/usr/local/lib/python3.5/site-packages/werkzeug/wrappers.py", line 26, in <module>
    from werkzeug.http import HTTP_STATUS_CODES, \
  File "/usr/local/lib/python3.5/site-packages/werkzeug/http.py", line 28, in <module>
    from urllib.request import parse_http_list as _parse_list_header
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 88, in <module>
    import http.client
  File "/Users/alexgurrola/Projects/intrepid/examples/http.py", line 2, in <module>
    from flask import Flask
ImportError: cannot import name 'Flask'

Most helpful comment

Please rename your file from http.py to something else, otherwise it clashes with the http module from the stdlib.

All 5 comments

Please provide an example to reproduce this.

Using Python 3.5.2 this paragraph in werkzeug/http.py will fail repeatedly.

try:
    from urllib.request import parse_http_list as _parse_list_header
    from urllib.parse import unquote_to_bytes as _unquote
except ImportError:  # pragma: no cover
    from urllib2 import parse_http_list as _parse_list_header, \
        unquote as _unquote

Also, the above script will complain on a deeper level without the try/except present, which alerted me to a possible change in the internal APIs with urllib. If it helps any, I found the issue originally when using Flask 0.12 and running the following script.

import os
from flask import Flask

app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])


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


if __name__ == '__main__':
    app.run()

Please rename your file from http.py to something else, otherwise it clashes with the http module from the stdlib.

Apparently Python can't differentiate between scripts of the same name, so it sent me on a wild goose chase through these libraries... I appreciate the help.

Please rename your file from http.py to something else, otherwise it clashes with the http module from the stdlib.

I'm new in Python, so I take the same mistake too....

Was this page helpful?
0 / 5 - 0 ratings