0

Am facing the below error while trying to import Flask. Similar questions posted had issues with Werkzeug versions

from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request

File "init__.py", line 25, in <module>
    from flask import Flask
File "user/anaconda/lib/python2.7/site-packages/flask/__init__.py", line 17,   
in <module>
    from werkzeug.exceptions import abort
File "user/anaconda/lib/python2.7/site-packages/werkzeug/__init__.py", line    
154, in <module>
    __import__('werkzeug.exceptions')
File "user/anaconda/lib/python2.7/site-packages/werkzeug/exceptions.py", line  
71, in <module>
    from werkzeug.wrappers import Response
File "user/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line  
26, in <module>
    from werkzeug.http import HTTP_STATUS_CODES, \
File "user/anaconda/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'
  1. Werkzeug version(I tried with 10.1,10.4 ) still the error remains.

  2. Someone pointed out that there might be a local copy of urllib2.. I figured out that there was both urllib and urllib2 indeed in my local path.. /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7

Now I am not sure how to proceed, Should I uninstall urllib/urllib2 ?

EDIT: The solution listed in Tried to use relative imports, and broke my import paths? doesn't work for me. In this case, I tried importing urllib2 into my Flask project and printing out the file path, but that throws the below exceptions

File "/user/__init__.py", line 25, in <module>
  import urllib2
File "/user/anaconda/lib/python2.7/urllib2.py", line 94, in <module>
  import httplib
File "/user/anaconda/lib/python2.7/httplib.py", line 80, in <module>
  import mimetools
File "/user/anaconda/lib/python2.7/mimetools.py", line 6, in <module>
  import tempfile
File "/user/anaconda/lib/python2.7/tempfile.py", line 32, in <module>
  import io as _io
File "/user/anaconda/lib/python2.7/io.py", line 51, in <module>
  import _io

ImportError: dlopen(/user/anaconda/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyErr_ReplaceException Referenced from: /user/anaconda/lib/python2.7/lib-dynload/_io.so Expected in: dynamic lookup

Community
  • 1
  • 1
bpt
  • 1
  • 2
  • http://stackoverflow.com/questions/17391289/tried-to-use-relative-imports-and-broke-my-import-paths might be a duplicate question – TehTris Jul 10 '15 at 21:18
  • The answer to that question did not address my issue. So I tried importing urllib2 from my flask project and printing the file path. Ideally it should give me the file path.. but I get the import error again – bpt Jul 10 '15 at 21:21
  • so what happened when you did `from urllib2 import parse_http_list as _parse_list_header` ? – TehTris Jul 10 '15 at 21:23
  • Hi, I've appeneded the error trace to my question – bpt Jul 10 '15 at 21:30
  • 1
    Maybe these two links may give you more ideas: http://stackoverflow.com/questions/19698509/flask-import-error-with-request-module http://stackoverflow.com/questions/3745771/urllib-request-in-python-2-7 – juanmajmjr Jul 10 '15 at 21:56

2 Answers2

1

I had a similar issue, and I followed the recommendation in the solution by adding from urllib2 import parse_http_list as _parse_list_header before from flask import Flask, then I got this error message:

"/Users/anaconda/lib/python2.7/io.py", line 51, in import _io 
ImportError: dlopen(/Users/anaconda/lib/python2.7/lib-dynload/_io.so, 2): 
Symbol not found: __PyCodecInfo_GetIncrementalDecoder 
Referenced from: /Users/anaconda/lib/python2.7/lib-dynload/_io.so 
Expected in: dynamic lookup

which I then resolved by following the solution:

  1. sudo find / -name _io.so
  2. I replaced /Users/anaconda/lib/python2.7/lib-dynload/_io.so with /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so

It works for me!

Community
  • 1
  • 1
DevEx
  • 4,337
  • 13
  • 46
  • 68
0

In my case, one of my python-modules named uu. Such as a file in python3-email module.

  ... line 4, in <module>
    import flask
  File "/usr/lib/python3/dist-packages/werkzeug/http.py", line 28, in <module>
    from urllib.request import parse_http_list as _parse_list_header
  File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
    import http.client
  File "/usr/lib/python3.4/http/client.py", line 69, in <module>
    import email.parser
  File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
    from email import message
  File "/usr/lib/python3.4/email/message.py", line 10, in <module>
    import uu

Then, i have the same error:

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

I hope, this example will help you.

Alexander Lubyagin
  • 1,346
  • 1
  • 16
  • 30