简体   繁体   English

从wsgi导入python失败

[英]Import python from wsgi fails

I've got a folder structure on an apache2/flask server that is this: 我在apache2 / flask服务器上具有以下文件夹结构:

/var/www/myapp
/var/www/myapp/routing.py
/var/www/myapp/__init__.py
/var/www/wsgi-scripts/myapp.wsgi

My application file (routing.py) in in myapp and is as follows: 我的应用程序文件(routing.py)在myapp中,如下所示:

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()

My myapp.wsgi that is in wsgi-scripts is this: 我在wsgi脚本中的myapp.wsgi是这样的:

import sys
sys.path.insert(0, '/var/www/myapp')
from myapp import routing as application

However when I load the page this is in my logs: 但是,当我加载页面时,这在我的日志中:

[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

WSGI is working correctly, I verified that with a testapp. WSGI工作正常,我使用testapp进行了验证。 What is going wrong? 怎么了?

So you intend to import a module routing from package myapp and call it as application . 因此,您打算从包myapp导入模块routing ,并将其称为application

  1. That is not matching your structure. 那与您的结构不匹配。 If you have /var/www/myapp in your path, you have to directly import the module routing . 如果您的路径中有/var/www/myapp ,则必须直接导入模块routing

  2. An application is not a module, it is a callable defined in the module. 应用程序不是模块,而是模块中可调用的定义。 It seems you'll have to do something like 看来您必须做类似的事情

     def theapp(<don't remember the argumets by heart>): app.debug = 'True' app.run() 

    and then the wsgi script can do 然后wsgi脚本可以

     from routing import theapp as application 

    But maybe I'm wrong here and you just have to do 但是也许我在这里错了,你只需要做

     from routing import app as application 

    as I have no experience with Flask. 因为我没有Flask的经验。

I was getting the same problem, solved it with the following snippet: 我遇到了同样的问题,并使用以下代码段解决了该问题:

ALLDIRS = ['/var/www/MarkerDB/']

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.path[:0] = new_sys_pat

Source: http://code.google.com/p/modwsgi/wiki/VirtualEnvironments#Application_Environments 来源: http : //code.google.com/p/modwsgi/wiki/VirtualEnvironments#Application_Environments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM