简体   繁体   English

在Flask和wsgi和Apache2中使用ImportError

[英]ImportError using Flask with wsgi and Apache2

I'm trying to set up a simple website using Flask, wsgi, and apache2. 我正在尝试使用Flask,wsgi和apache2建立一个简单的网站。 I'm getting the following error trying to import from site.py into site.wsgi: 我在尝试从site.py导入site.wsgi时遇到以下错误:

[Fri Jan 11 16:42:20 2013] [error] [client 174.48.34.188] mod_wsgi (pid=15170): Target WSGI script '/home/www/site/site.wsgi' cannot be loaded as Python module.
[Fri Jan 11 16:42:20 2013] [error] [client 174.48.34.188] mod_wsgi (pid=15170): Exception occurred processing WSGI script '/home/www/site/site.wsgi'.
[Fri Jan 11 16:42:20 2013] [error] [client 174.48.34.188] Traceback (most recent call last):
[Fri Jan 11 16:42:20 2013] [error] [client 174.48.34.188]   File "/home/www/site/site.wsgi", line 1, in <module>
[Fri Jan 11 16:42:20 2013] [error] [client 174.48.34.188]     from site import app as application
[Fri Jan 11 16:42:20 2013] [error] [client 174.48.34.188] ImportError: cannot import name app

Here is my site.py: 这是我的site.py:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home_page():
    return render_template('index.html')

app.debug = True
if __name__ == '__main__'
    app.run()

Here is my site.wsgi: 这是我的site.wsgi:

from site import app as application

And here is my apache config: 这是我的Apache配置:

<VirtualHost *:80>
    ServerAdmin my@email.here
    ServerName mywebsite.here
    DocumentRoot /home/www/site

    WSGIDaemonProcess site user=${APACHE_RUN_USER} group=${APACHE_RUN_GROUP} threads=5
    WSGIScriptAlias / /home/www/site/site.wsgi

    <Directory /home/www/site>
        WSGIProcessGroup site
        WSGIApplicationGroup %{GLOBAL}
        WSGIScriptReloading On
        Order deny,allow
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
</VirtualHost>

I've searched around for a while trying to figure this out but I'm stumped. 我已经搜寻了一段时间,试图找出答案,但是我很困惑。 I'm also fairly new to web design, so it could be something silly. 我对网页设计也很陌生,所以可能有些愚蠢。 Thanks in advance. 提前致谢。

Found the answer - as Audrius said in the comments, it was a conflict because I named the file site.py. 找到了答案-正如Audrius在评论中所说,这是冲突,因为我将文件命名为site.py。 I changed it to mysite.py everywhere necessary and added the following to mysite.wsgi (formerly site.wsgi): 我在必要时将其更改为mysite.py,并将以下内容添加到mysite.wsgi(以前称为site.wsgi)中:

import sys
sys.path.insert(0, '/home/www/mysite.wsgi')

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

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