尝试使用Python 3运行Flask时,“ImportError:没有名为SocketServer的模块”

时间:2015-05-28 15:19:52

标签: python python-3.x flask

Flask已安装pip3 install flask。我的代码如下:

import flask
app = flask.Flask(__name__)

@app.route('/')
def hello():
    return "Hello World"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

当我以Python 2运行此代码时,它会生成通常的输出并正确响应请求。在Python 3下运行时,它不会产生任何输出,并且所有连接到localhost:8080或127.0.0.1:8080的尝试都被拒绝。

当我杀死服务器时,它会给我这样的信息:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 58, in <module>
    from SocketServer import ThreadingMixIn, ForkingMixIn
ImportError: No module named 'SocketServer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "testflask.py", line 9, in <module>
    app.run(host='0.0.0.0', port=8080)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 758, in run
    from werkzeug.serving import run_simple
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 61, in <module>
    from socketserver import ThreadingMixIn, ForkingMixIn
  File "/home/samtheman/code/lasreader/rclick/socketserver.py", line 25, in <module>
    MyServer(s.accept()).start()
  File "/usr/lib/python3.4/socket.py", line 184, in accept
    fd, addr = self._accept()
KeyboardInterrupt
$ pip3 show flask werkzeug
---
Name: Flask
Version: 0.10.1
Location: /usr/local/lib/python3.4/dist-packages
Requires: Werkzeug, Jinja2, itsdangerous
---
Name: Werkzeug
Version: 0.10.4
Location: /usr/local/lib/python3.4/dist-packages
Requires: 
$ pip show flask werkzeug
---
Name: Flask
Version: 0.10.1
Location: /usr/local/lib/python2.7/dist-packages
Requires: Werkzeug, Jinja2, itsdangerous
---
Name: Werkzeug
Version: 0.9.6
Location: /usr/local/lib/python2.7/dist-packages
Requires: 

1 个答案:

答案 0 :(得分:2)

您的追溯显示正在导入错误的模块

  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 61, in <module>
    from socketserver import ThreadingMixIn, ForkingMixIn
  File "/home/samtheman/code/lasreader/rclick/socketserver.py", line 25, in <module>
    MyServer(s.accept()).start()

在那里看到第二条File行?那不是标准库socketserver,它们完全是一个不同的模块。作为该模块的一部分,它在导入时启动套接字服务器,因此Werkzeug导入永远不会完成,永远无法正常运行。

从python路径中删除/home/samtheman/code/lasreader/rclick或完全删除该模块。