Python從wsgi導(dǎo)入失敗的問(wèn)題解決方法
在一個(gè)apache2/flask服務(wù)器上,有一個(gè)文件夾結(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)我加載頁(yè)面時(shí),日志中出現(xiàn)以下錯(cuò)誤:
[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
解決方案
問(wèn)題的原因是Python從wsgi導(dǎo)入失敗,可能是因?yàn)橐韵略颍?/p>
- myapp.routing模塊沒(méi)有被正確導(dǎo)入。
- myapp.routing模塊沒(méi)有被正確定義。
方法一:
第一個(gè)答案中給出的解決方案是:
- 直接導(dǎo)入routing模塊,而不是myapp.routing模塊。
- 將application定義為一個(gè)函數(shù),而不是一個(gè)模塊。
代碼示例:
# 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()
方法二:
第二個(gè)答案中給出的解決方案是:
- 使用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)入失敗的問(wèn)題解決方法的文章就介紹到這了,更多相關(guān)Python wsgi導(dǎo)入失敗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python?Web開(kāi)發(fā)通信協(xié)議WSGI?uWSGI?uwsgi使用對(duì)比全面介紹
- Python安裝和配置uWSGI的詳細(xì)過(guò)程
- 通過(guò)Python中的CGI接口講解什么是WSGI
- django生產(chǎn)環(huán)境搭建(uWSGI+django+nginx+python+MySQL)
- Python WSGI 規(guī)范簡(jiǎn)介
- 淺析Python 中的 WSGI 接口和 WSGI 服務(wù)的運(yùn)行
- Docker構(gòu)建python Flask+ nginx+uwsgi容器
- python web框架 django wsgi原理解析
- Python開(kāi)發(fā)之Nginx+uWSGI+virtualenv多項(xiàng)目部署教程
相關(guān)文章
Python descriptor(描述符)的實(shí)現(xiàn)
這篇文章主要介紹了Python descriptor(描述符)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python連接mongodb密碼認(rèn)證實(shí)例
今天小編就為大家分享一篇python連接mongodb密碼認(rèn)證實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Face++ API實(shí)現(xiàn)手勢(shì)識(shí)別系統(tǒng)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Face++ API實(shí)現(xiàn)手勢(shì)識(shí)別系統(tǒng)設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
python日期時(shí)間轉(zhuǎn)為字符串或者格式化輸出的實(shí)例
今天小編就為大家分享一篇python日期時(shí)間轉(zhuǎn)為字符串或者格式化輸出的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
numpy:np.newaxis 實(shí)現(xiàn)將行向量轉(zhuǎn)換成列向量
今天小編就為大家分享一篇numpy:np.newaxis 實(shí)現(xiàn)將行向量轉(zhuǎn)換成列向量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
解決Windows下PowerShell無(wú)法進(jìn)入Python虛擬環(huán)境問(wèn)題
這篇文章主要介紹了解決Windows下PowerShell無(wú)法進(jìn)入Python虛擬環(huán)境問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
pytest-sugar?執(zhí)行過(guò)程中顯示進(jìn)度條的腳本分享
Pytest-sugar是一款用來(lái)改善控制臺(tái)顯示的插件,增加了進(jìn)度條顯示,使得在用例執(zhí)行過(guò)程中可以看到進(jìn)度條,而且進(jìn)度條是根據(jù)用例是否通過(guò)標(biāo)注不同顏色,非常醒目,接下來(lái)通過(guò)本文給大家分享下pytest?sugar?顯示進(jìn)度條的腳本,感興趣的朋友一起看看吧2022-12-12
Python程序打包成EXE的四種方法詳解與實(shí)戰(zhàn)
將Python代碼打包成可執(zhí)行文件是一種使你的應(yīng)用程序更易于分享和分發(fā)的方法,本文一步一步地教你如何用 Pyinstaller 模塊將Python程序打包成exe文件,這篇教程絕對(duì)是全網(wǎng)最全面、最詳細(xì)的教程,包含四種打包的方法,需要的朋友可以參考下2025-07-07

