2013-12-11 75 views
0

我正在使用Flask構建移動應用程序。在我爲大型應用程序設置Flask結構之前,它工作正常。像這樣http://flask.pocoo.org/docs/patterns/packages/Flask無法導入WSGI上的Python文件

當我有一個python文件時WSGI工作的很好。但是,在導入python文件後,WSGI不工作。

這裏是的目錄結構。

/home 
    /bridge 
    index.py 
    apis.wsgi 
    /apis 
     __init__.py 
     settings.py 

Index.py

from apis import app 
app.run(host='0.0.0.0',debug=True) 

apis.wsgi

import sys 
sys.path.insert(0,'/home/bridge/apis') 
from apis import app as application 

初始化的.py

from flask import Flask 
app = Flask(__name__) 

import apis.settings 

settings.py

from flask import Flask 
import os 
import sys 
from flask import render_template, request, jsonify, redirect, url_for, send_file, Response, make_response 

@app.route('/apis/settings/aboutus') 
def aboutus(): 
    return render_template('settings/aboutus.html') 

當我運行#python後運行它index.py Domain.com/apis/settings/aboutus工作的罰款。

但是,如果我打開WSGI,則無法加載頁面並顯示此錯誤。

Internal Server Error 

當然,我檢查錯誤日誌。

[error] [client 14.63.12.134] mod_wsgi (pid=2138): Exception occurred processing WSGI script '/home/bridge/index.wsgi'. 
[error] [client 14.63.12.134] Traceback (most recent call last): 
[error] [client 14.63.12.134] File "/home/bridge/index.wsgi", line 3, in <module> 
[error] [client 14.63.12.134]  from index import app as application 
[error] [client 14.63.12.134] File "/home/bridge/index.py", line 2, in <module> 
[error] [client 14.63.12.134]  app.run(host='0.0.0.0',debug=True) 
[error] [client 14.63.12.134] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run 
[error] [client 14.63.12.134]  run_simple(host, port, self, **options) 
[error] [client 14.63.12.134] File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 706, in run_simple 
[error] [client 14.63.12.134]  test_socket.bind((hostname, port)) 
[error] [client 14.63.12.134] File "/usr/lib/python2.7/socket.py", line 224, in meth 
[error] [client 14.63.12.134]  return getattr(self._sock,name)(*args) 
[error] [client 14.63.12.134] error: [Errno 98] Address already in use 

我得到這些錯誤。

你能看到有什麼問題嗎?謝謝。

回答

4

正確index.py到:

from apis import app 

if __name__ == "__main__": 
    app.run(host='0.0.0.0',debug=True) 

因爲WSGI服務器會打電話給你app,所以你不必再打電話app.run做,否則就是「地址已在使用」引發錯誤。所以我們使用if __name__ == "__main__":來包裝app.run(...)flask docs也指出了這個訣竅。

0

我簡單地解決了這個問題。

index.py

from apis import app 
#app.run(host='0.0.0.0',debug=True) 

我刪除運行命令,和它的工作。當WSGI mod嘗試運行它時,app.run命令已經運行。

相關問題