我可以使用Flask + WSGI成功运行一个“ hello world”应用程序...但是在“文件夹”中使用“ routes.py”更改我的项目结构会使服务器出现错误...
mod_wsgi(pid = 9):目标WSGI脚本'/var/www/myfirstapp/hello.wsgi'无法作为Python模块加载。
mod_wsgi(pid = 9):处理WSGI脚本'/var/www/myfirstapp/hello.wsgi'时发生异常
从folder.routes导入simple_page
ImportError:没有名为folder.routes的模块
这是我的项目树:
├── folder
│ └── routes.py
├── hello.conf
├── hello.py
├── hello.wsgi
└── README.md
hello.py:
from flask import Flask
from folder.routes import simple_page # works in dev but not with wsgi.. Why?
app = Flask(__name__)
app.register_blueprint(simple_page)
route.py:
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
simple_page = Blueprint('simple_page', __name__,
template_folder='templates')
@simple_page.route('/')
def index():
try:
return "Hello world"
except TemplateNotFound:
abort(404)
hello.conf
<VirtualHost *>
ServerName example.com
WSGIScriptAlias / /var/www/myfirstapp/hello.wsgi
WSGIDaemonProcess hello python-path=/var/www/myfirstapp:/var/www/myfirstapp/.env/lib/python3.5/site-packages
<Directory /var/www/myfirstapp>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
hello.wsgi:
import sys
sys.path.insert(0, "/var/www/myfirstapp")
from hello import app as application
注意:我不明白为什么WSGI在根文件夹中的“ routes.py”(导入路由)是“好”的,但是却抱怨(hello.py)在hello中导入“ folder.routes”。 py,如果我将相同的文件放在“文件夹” ...
答案 0 :(得分:0)
我找到了一个解决方案,引用了WSGIDaemonProcess hello.conf行中的相应模块(文件夹):
pythonpath=/var/www/myfirstapp/.env/lib/python3.5/sitepackages:/var/www/myfirstapp/:/var/www/myfirstapp/folder
在routes.py中:
而不是from folder.routes import simple_page
,我们有了import routes
。
项目树也发生了一些变化:
├── myfirstapp
│ ├── folder
│ │ └── routes.py
│ └── __init__.py
├── hello.wsgi
└── README.md