[英]“ImportError: No module named SocketServer” when trying to run Flask with Python 3
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:
您的回溯表明導入了錯誤的模塊 :
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
, socketserver
完全不同的模塊。 作為該模塊的一部分,它將在import上啟動套接字服務器,因此Werkzeug導入永遠不會完成,也永遠無法正常運行。
從python路徑中刪除/home/samtheman/code/lasreader/rclick
或完全刪除該模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.