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, I tried to get fancy with some relative imports to test a class in a different directory:
In project/testing/something/test_class.py:
from ..my_lib.my_class import MyClass
That gave me an error:
ValueError: Attempted relative import beyond toplevel package
So I backed out of that, but now I can't get my Flask server to run, even though I eliminated the new import code.
$ python testing/flask_server.py
Spits out this:
Traceback (most recent call last):
File "testing/flask_server.py", line 2, in <module>
from flask import Flask
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/__init__.py", line 154, in <module>
__import__('werkzeug.exceptions')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/exceptions.py", line 71, in <module>
from werkzeug.wrappers import Response
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/wrappers.py", line 26, in <module>
from werkzeug.http import HTTP_STATUS_CODES, \
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/werkzeug/http.py", line 28, in <module>
from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request
Huh?
UPDATE:
The flask import error only happens in the directory the bad code was originally called from. That is, if I do
from flask import Flask
from anywhere in the project/testing dir, I get the import error, but if I do it in project/ or anywhere else on my system, it's fine..?
SOLUTION (PARTLY):
I don't have an explanation as to why this happened, but I did the following to fix it:
- Created a new testing/ directory and copied the files from the old one into it. The old testing/ directory had to be deleted- it was basically corrupted.
- Did my cross-directory imports using absolute instead of relative paths.
(By the way, I tried to retrace my steps to reproduce the relative import error but was unable to, so I'm not sure of either the cause or solution to this whole thing.)