Skip to content Skip to sidebar Skip to footer

Tried To Use Relative Imports, And Broke My Import Paths?

I'm running a Flask server locally on my Mac. My project: project/my_lib/my_class.py project/testing/flask_server.py project/testing/something/test_class.py At one point,

Solution 1:

You probably have your own urllib2 python file in your system path, perhaps in the local directory. Don't do that, as it breaks werkzeug (and other python code).

To be compatible with both python 2 and 3, werkzeug uses constructs like:

try:
    from urllib2 import parse_http_list as _parse_list_header
except ImportError: # pragma: no cover
    from urllib.request import parse_http_list as _parse_list_header

The from urllib2 import parse_http_list as _parse_list_header line could throw a ImportError exception if you have a local urllib2.py module or urllib2/__init__.py package that masks the standard library file.

Because the first import throws an ImportError, the second line is executed, which also fails because the urllib.request package is only available on Python 3.

From your project, run the following code to diagnose where you have that module:

import urllib2
print urllib2.__file__

If that still works, then run:

from urllib2 import parse_http_list as _parse_list_header

as it could be that urllib2 indirectly imports something that you masked. urllib2 uses from urlib import ... statements for example, so a local urllib module would also break the import.

It is important that you do this from your flask project, just before the from flask import Flask line.


Post a Comment for "Tried To Use Relative Imports, And Broke My Import Paths?"