问题描述
我正在尝试使用 flask run
运行Flask应用程序,但是无论如何,我都会收到此错误:
I'm trying to run a Flask application with flask run
but no matter what, I receive this error:
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
我在项目中使用virtualenv,并且在端口80上运行应用程序,因此我以超级用户身份运行命令.最终,我只需要使用Flask-Migrate的文档,但 flask
需要能够找到要执行此操作的应用.这是我尝试过的,但没有成功:
I'm using virtualenv in my project and I'm running the app on port 80 so I run the command as superuser. Ultimately, I just need to use the flask db init
command as described in Flask-Migrate's docs, but flask
needs to be able to find the app to do that. Here's what I've tried, with no success:
导出 FLASK_APP
环境变量,确保它位于我的bash配置文件中,然后激活virtualenv
Exporting the FLASK_APP
environment variable, ensuring that it's in my bash profile, then activating virtualenv
$ export FLASK_APP=run.py
$ printenv FLASK_APP
run.py
$ . env/bin/activate
(env) $ sudo flask run
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
激活virtualenv,然后导出 FLASK_APP
Activating virtualenv, then exporting FLASK_APP
$ . env/bin/activate
(env) $ export FLASK_APP=run.py
(env) $ printenv FLASK_APP
run.py
(env) sudo flask run
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
以上两个具有完整路径,/Users/me/code/project/run.py
The above two with the full path, /Users/me/code/project/run.py
$ printenv FLASK_APP
/Users/me/code/project/run.py
项目结构
myproject/
├──app/
| ├── __init__.py
| ├── models.py
| ├── templates/
| └── views.py
├── tests/
├── run.py
├── requirements.txt
└── config.py
到目前为止,没有任何效果,并且每种情况下的错误消息都是相同的.我该怎么办才能解决此错误?
So far nothing has worked and the error message is the same in each case. What can I do to fix this error?
推荐答案
当我从 sudo flask run
中放下 sudo
时,Flask找到了 $ FLASK_APP
.但是,我收到错误消息 socket.error:[Errno 13]权限被拒绝
.我找不到解决方法,因为当我以超级用户身份运行时,Flask无法找到 $ FLASK_APP
.好像是循环逻辑.
When I drop sudo
from sudo flask run
, Flask finds $FLASK_APP
. However, I get the error message socket.error: [Errno 13] Permission denied
. I can't see a way around this, as Flask cannot find $FLASK_APP
when I run as superuser. Seems like circular logic.
我设法通过将端口从80更改为5000并使用 flask run
删除 sudo
来运行Flask.很好,我将不得不找到一种在生产中的端口80上运行该应用程序的方法.
I've managed to run Flask by changing the port from 80 to 5000 and dropping sudo
with flask run
. This is fine, I will have to find a way to run the app on port 80 in production though.
在删除并重新创建数据库并删除对 db.create_all
的调用之后,我能够运行 flask db init
.
I was able to run flask db init
after dropping and recreating my database, and removing calls to db.create_all
.
Edit-4/27/17 端口80实际上已被服务器上的防火墙阻止(防火墙不受我控制),因此在开放端口上运行该应用程序即可解决此问题.
Edit - 4/27/17 port 80 was indeed blocked by the firewall on my server (the firewall is beyond my control) so running the app on an open port resolved the issue.
这篇关于Flask说“未提供FLASK_APP环境变量".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!