我正在尝试在Ubuntu16.04上使用Python3 / apache2运行Flask应用程序。但是,如果我尝试在FlaskApp的 init .py中导入file.py,就会遇到错误。
我的应用程序结构: \ var \ www \ FlaskApp:
|- FlaskApp\ |- templates\ |- static\ |- content_management.py |- __init__.py |- FlaskApp.wsgi
以下是错误的印刷说明:
root@ordi:~# service apache2 restart
root@ordi:~# sudo tail /var/log/apache2/error.log
[Thu Aug 09 13:59:58.463465 2018] [wsgi:error] [pid 7535:tid 139662857328384] [client ] import content_management, referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.463497 2018] [wsgi:error] [pid 7535:tid 139662857328384] [client ] ImportError: No module named 'content_management', referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.780434 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] mod_wsgi (pid=7534): Target WSGI script '/var/www/FlaskApp/FlaskApp.wsgi' cannot be loaded as Python module ., referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.780535 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] mod_wsgi (pid=7534): Exception occurred processing WSGI script '/var/www/FlaskApp/FlaskApp.wsgi'., referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.780941 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] Traceback (most recent call last):, referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.780973 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] File "/var/www/FlaskApp/FlaskApp.wsgi", line 7, in <module>, referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.780978 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] from FlaskApp import app as application, referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.780985 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 2, in <module>, referer: http://www.MyWebSite.com/dashboar d/
[Thu Aug 09 13:59:58.780988 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] import content_management, referer: http://www.MyWebSite.com/dashboard/
[Thu Aug 09 13:59:58.781007 2018] [wsgi:error] [pid 7534:tid 139662790186752] [client ] ImportError: No module named 'content_management', referer: http://www.MyWebSite.com/dashboard/
root@ordi:~# mod_wsgi (pid=7534): Target WSGI script '/var/www/FlaskApp/FlaskApp.wsgi' cannot be loaded as Python module
FlaskApp.wsgi:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'SecretKey'
初始化 .py:
from flask import Flask, render_template, flash
from content_management import content
TOPIC_DICT = Content()
app = Flask(__name__)
@app.route('/')
def homepage():
return render_template("main.html")
@app.route('/dashboard/')
def dashboard():
return render_template("dashboard.html", TOPIC_DICT = TOPIC_DICT)
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html")
if __name__ == "__main__":
app.run()
我可以在不到一个小时的时间内重置服务器并重新安装它,但是我想了解问题所在。 为什么我的 init .py文件无法导入content_management.py文件? 我怎么解决这个问题?我应该到处写sys.path.insert()并祈祷吗?