0

我在 apache2/flask 服务器上有一个文件夹结构,它是这样的:

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

我的应用程序文件(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()

我在 wsgi-scripts 中的 myapp.wsgi 是这样的:

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

但是,当我加载页面时,这在我的日志中:

[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 工作正常,我用 testapp 验证了这一点。出了什么问题?

4

2 回答 2

2

So you intend to import a module routing from package myapp and call it as 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.

  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

    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.

于 2013-03-15T08:18:33.440 回答
1

我遇到了同样的问题,使用以下代码段解决了它:

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

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

于 2013-11-14T02:19:34.423 回答