Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError causing Exception while handling an Exception #1050

Closed
alexgurrola opened this issue Dec 22, 2016 · 5 comments
Closed

ImportError causing Exception while handling an Exception #1050

alexgurrola opened this issue Dec 22, 2016 · 5 comments

Comments

@alexgurrola
Copy link

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'
@davidism
Copy link
Member

Please provide an example to reproduce this.

@alexgurrola
Copy link
Author

alexgurrola commented Dec 24, 2016

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()

@untitaker
Copy link
Contributor

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

@alexgurrola
Copy link
Author

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.

@lby1992
Copy link

lby1992 commented Mar 29, 2019

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....

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants