Python從wsgi導(dǎo)入失敗的問題解決方法
在一個apache2/flask服務(wù)器上,有一個文件夾結(jié)構(gòu)如下:
/var/www/myapp /var/www/myapp/routing.py /var/www/myapp/__init__.py /var/www/wsgi-scripts/myapp.wsgi
myapp文件夾中的應(yīng)用程序文件(routing.py)如下:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def welkom(): return render_template('welkom.html') if __name__ == '__main__': app.debug = 'True' app.run()
wsgi-scripts中的myapp.wsgi文件如下:
import sys sys.path.insert(0, '/var/www/myapp') from myapp import routing as application
但是當(dāng)我加載頁面時,日志中出現(xiàn)以下錯誤:
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23574): Target WSGI script '/var/www/wsgi-scripts/myapp.wsgi' cannot be loaded as Python module. [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23574): Exception occurred processing WSGI script '/var/www/wsgi-scripts/myapp.wsgi'. [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] Traceback (most recent call last): [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] File "/var/www/wsgi-scripts/myapp.wsgi", line 3, in <module> [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] import myapp.routing as application [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] ImportError: No module named myapp.routing [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23575): Target WSGI script '/var/www/wsgi-scripts/myapp.wsgi' cannot be loaded as Python module. [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23575): Exception occurred processing WSGI script '/var/www/wsgi-scripts/myapp.wsgi'. [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] Traceback (most recent call last): [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] File "/var/www/wsgi-scripts/myapp.wsgi", line 3, in <module> [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] import myapp.routing as application [Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] ImportError: No module named myapp.routing [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23577): Target WSGI script '/var/www/wsgi-scripts/myapp.wsgi' cannot be loaded as Python module. [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23577): Exception occurred processing WSGI script '/var/www/wsgi-scripts/myapp.wsgi'. [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] Traceback (most recent call last): [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] File "/var/www/wsgi-scripts/myapp.wsgi", line 3, in <module> [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] import myapp.routing as application [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] ImportError: No module named myapp.routing [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23573): Target WSGI script '/var/www/wsgi-scripts/myapp.wsgi' cannot be loaded as Python module. [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23573): Exception occurred processing WSGI script '/var/www/wsgi-scripts/myapp.wsgi'. [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] Traceback (most recent call last): [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] File "/var/www/wsgi-scripts/myapp.wsgi", line 3, in <module> [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] import myapp.routing as application [Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] ImportError: No module named myapp.routing
解決方案
問題的原因是Python從wsgi導(dǎo)入失敗,可能是因為以下原因:
- myapp.routing模塊沒有被正確導(dǎo)入。
- myapp.routing模塊沒有被正確定義。
方法一:
第一個答案中給出的解決方案是:
- 直接導(dǎo)入routing模塊,而不是myapp.routing模塊。
- 將application定義為一個函數(shù),而不是一個模塊。
代碼示例:
# myapp.wsgi import sys sys.path.insert(0, '/var/www/myapp') from routing import app as application
# routing.py def app(environ, start_response): app.debug = 'True' app.run()
方法二:
第二個答案中給出的解決方案是:
- 使用site.addsitedir()函數(shù)將myapp目錄添加到Python的包路徑中。
- 使用sys.path[:] = new_sys_path將新路徑放在Python的包路徑的前面。
代碼示例:
# myapp.wsgi ALLDIRS = ['/var/www/myapp/'] import sys import site # Remember original sys.path. prev_sys_path = list(sys.path) # Add each new site-packages directory. for directory in ALLDIRS: site.addsitedir(directory) # Reorder sys.path so new directories at the front. new_sys_path = [] for item in list(sys.path): if item not in prev_sys_path: new_sys_path.append(item) sys.path.remove(item) sys.
到此這篇關(guān)于Python從wsgi導(dǎo)入失敗的問題解決方法的文章就介紹到這了,更多相關(guān)Python wsgi導(dǎo)入失敗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python?Web開發(fā)通信協(xié)議WSGI?uWSGI?uwsgi使用對比全面介紹
- Python安裝和配置uWSGI的詳細(xì)過程
- 通過Python中的CGI接口講解什么是WSGI
- django生產(chǎn)環(huán)境搭建(uWSGI+django+nginx+python+MySQL)
- Python WSGI 規(guī)范簡介
- 淺析Python 中的 WSGI 接口和 WSGI 服務(wù)的運行
- Docker構(gòu)建python Flask+ nginx+uwsgi容器
- python web框架 django wsgi原理解析
- Python開發(fā)之Nginx+uWSGI+virtualenv多項目部署教程
相關(guān)文章
安裝Keras,tensorflow,并實現(xiàn)將虛擬環(huán)境添加到j(luò)upyter?notebook
這篇文章主要介紹了安裝Keras,tensorflow,并實現(xiàn)將虛擬環(huán)境添加到j(luò)upyter?notebook,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03如何利用飾器實現(xiàn) Python 函數(shù)重載
這篇文章主要介紹了如何利用飾器實現(xiàn) Python 函數(shù)重載,需要的朋友可以參考下面文章內(nèi)容,希望能幫助到你2021-09-09python如何用pymodbus庫進(jìn)行modbus tcp通信
這篇文章主要介紹了python如何用pymodbus庫進(jìn)行modbus tcp通信問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06python 爬蟲基本使用——統(tǒng)計杭電oj題目正確率并排序
這篇文章主要介紹了python 爬蟲基本的基本使用,主要利用了Urllib和BeautifulSoup4這兩個庫,配以簡單的實例幫助大家理解,感興趣的朋友可以了解下2020-10-10django model 條件過濾 queryset.filter(**condtions)用法詳解
這篇文章主要介紹了django model 條件過濾 queryset.filter(**condtions)用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05pytorch和tensorflow計算Flops和params的詳細(xì)過程
這篇文章主要介紹了pytorch和tensorflow計算Flops和params,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08關(guān)于windows下Tensorflow和pytorch安裝教程
Tensorflow是廣泛使用的實現(xiàn)機器學(xué)習(xí)以及其它涉及大量數(shù)學(xué)運算的算法庫之一。這篇文章主要介紹了Tensorflow和pytorch安裝(windows安裝),需要的朋友可以參考下2020-02-02