我尝试了非常简单的python,但不知道错误从何而来。
__init__.py
(位置:/var/www/project/stuff/__init__.py
)
from flask import Flask
app = Flask(__name__)
home.py
(位置:/var/www/project/stuff/home.py
)
from stuff import app
@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
return "This is Home"
run.py
(位置:/var/www/project/run.py
)
from stuff import app
if __name__ == "__main__":
app.run(debug==True)
我遇到ModuleNoFoundError: No module named 'stuff'
异常。
我在做什么错了?
这是我的project.wsgi
(位置:/var/www/project
)
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/project/stuff/")
from home import app as application
和回溯是
[Thu Nov 22 12:54:06.145378 2018] [wsgi:error] File "/var/www/project/project.wsgi", line 6, in <module>
[Thu Nov 22 12:54:06.145384 2018] [wsgi:error] from home import app as application
[Thu Nov 22 12:54:06.145390 2018] [wsgi:error] File "/var/www/project/stuff/home.py", line 1, in <module>
[Thu Nov 22 12:54:06.145394 2018] [wsgi:error] from stuff import app
[Thu Nov 22 12:54:06.145407 2018] [wsgi:error] ModuleNotFoundError: No module named 'stuff'
[Thu Nov 22 12:54:06.227614 2018] [wsgi:error] mod_wsgi (pid=3172): Target WSGI script '/var/www/project/project.wsgi' cannot be loaded as Python module., referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227668 2018] [wsgi:error] mod_wsgi (pid=3172): Exception occurred processing WSGI script '/var/www/project/project.wsgi'., referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227898 2018] [wsgi:error] Traceback (most recent call last):, referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227927 2018] [wsgi:error] File "/var/www/project/project.wsgi", line 6, in <module>, referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227933 2018] [wsgi:error] from home import app as application, referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227940 2018] [wsgi:error] File "/var/www/project/stuff/home.py", line 1, in <module>, referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227944 2018] [wsgi:error] from stuff import app, referer: http://52.195.0.108/
[Thu Nov 22 12:54:06.227957 2018] [wsgi:error] ModuleNotFoundError: No module named 'stuff', referer: http://52.195.0.108/
答案 0 :(得分:0)
您要在模块搜索路径中添加软件包目录:
sys.path.insert(0,"/var/www/project/stuff/")
不要这样做。 Python在stuff
中找不到/var/www/project/stuff/
包。您应该导入stuff.home
,而不是home
:
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/project")
from stuff.home import app as application
您从app
导入home
有点..向后。您可能需要重组项目,以在单独的子模块中创建Flask app
对象和起始路径:
stuff/__init__.py
:
from . import app, home
application = app.app
stuff/app.py
:
from flask import Flask
app = Flask(__name__)
stuff/home.py
:
from .app import app
@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
return "This is Home"
您可以完全删除run.py
。开发时,请使用flask
命令行应用程序运行代码:
FLASK_APP=stuff flask run
我使用.
相对于当前软件包进行导入,因此您可以更轻松地将stuff
重命名为其他名称。
答案 1 :(得分:-1)
当我使用
home.py(位置:/var/www/project/stuff/home.py)
from flask import Flask
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
return "This is Home"
run.py(位置:/var/www/project/run.py)
from stuff import app
if __name__ == "__main__":
app.run(debug==True)
这有效