Flask中的导入错误(本地路径中的urllib)

时间:2015-07-10 21:12:18

标签: python flask urllib2 urllib importerror

尝试导入Flask时遇到以下错误。发布的类似问题与Werkzeug版本存在问题

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版本(我尝试使用10.1,10.4)仍然存在错误。

  2. 有人指出可能有urllib2的本地副本..我 我发现我的确存在了urllib和urllib2 当地的道路.. /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7

  3. 现在我不知道如何继续,我应该卸载urllib / urllib2吗?

    编辑:Tried to use relative imports, and broke my import paths?中列出的解决方案对我不起作用。在这种情况下,我尝试将urllib2导入我的Flask项目并打印出文件路径,但是会抛出以下异常

    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):找不到符号:__PyErr_ReplaceException   参考自:/user/anaconda/lib/python2.7/lib-dynload/_io.so   期望:动态查找

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,我在from urllib2 import parse_http_list as _parse_list_header之前添加了from flask import Flask,然后我收到了此错误消息:

"/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

然后我按照solution

解决了这个问题
  1. sudo find / -name _io.so
  2. 我用/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2替换了/Users/anaconda/lib/python2.7/lib-dynload/_io.so 0.7 / LIB-dynload / _io.so
  3. 对我有用!

答案 1 :(得分:0)

就我而言,是我的一个名为uu的python模块。例如python3-email模块中的文件。

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

然后,我有同样的错误:

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'

希望此示例对您有所帮助。