Python 环境 运行 Minimal Flask 应用程序时未设置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37826016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Env. Variables not set while running Minimal Flask application
提问by Batool
I am trying to follow the flask documentation on my windows machine given at the following link: http://flask.pocoo.org/docs/0.11/quickstart/#debug-mode
我正在尝试按照以下链接提供的 Windows 机器上的烧瓶文档进行操作:http: //flask.pocoo.org/docs/0.11/quickstart/#debug-mode
Firstly I wrote the code below in a python script:
首先,我在 python 脚本中编写了以下代码:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
I saved this in a file called run.py
我把它保存在一个名为 run.py
Then wrote this command in the command window:
然后在命令窗口写下这个命令:
set FLASK_APP = run.py
flask run
Upon running this, I am getting the following error:
运行此程序后,我收到以下错误:
"Error: Could not locate flask application. You did not provide the FLASK_APP environment variable"
"Error: Could not locate flask application. You did not provide the FLASK_APP environment variable"
I was expecting to get this instead:
我期待得到这个:
Running on http://127.0.0.1:5000/
Can somebody tell me how to fix this?
有人可以告诉我如何解决这个问题吗?
回答by Folky
I faced the same issue while using PowerShell and that fix worked for me:
instead of using set FLASK_APP = run.py
, try $env:FLASK_APP = "run.py"
我在使用 PowerShell 时遇到了同样的问题,该修复程序对我有用:
不要使用set FLASK_APP = run.py
,请尝试$env:FLASK_APP = "run.py"
回答by Inzamam Malik
If you are using powershell it does not works i dont know why
please use cmd.exe
since i use VScode editor it provide powershell as a terminal(ctrl+
) by default so i was trying to run flask app on the powershell and it was giving me same response as you are getting
如果您使用的是 powershell,它不起作用,我不知道为什么请使用,cmd.exe
因为我使用 VScode 编辑器ctrl+
,默认情况下它提供 powershell 作为终端(),所以我试图在 powershell 上运行Flask应用程序,它给了我与您相同的响应正在得到
1) open cmd.exe (or if you are VSCodeuser like me simply write cmd
on that terminal)
1)打开cmd.exe(或者如果你像我一样是VSCode用户,只需cmd
在那个终端上写)
2) set FLASK_APP=hello.py
(without spaces, only for first run then it remember until restart of cmd)
2)set FLASK_APP=hello.py
(没有空格,仅用于第一次运行然后它会记住直到重新启动cmd)
3) flask run
(or just flask will also work)
3)flask run
(或者只是烧瓶也可以)
note: this is only for windows user
注意:这仅适用于 Windows 用户
回答by crifan
export FLASK_APP=run.py
flask run --host=0.0.0.0
export FLASK_APP=run.py
flask run --host=0.0.0.0
then can run normally, output something like:
然后就可以正常运行了,输出如下:
* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 353-795-063
回答by Vikrant Shitole
I wonder why this is missing from the answer.
我想知道为什么答案中缺少这一点。
Here is a simple solution:
这是一个简单的解决方案:
add app.run(port=5000)
添加 app.run(port=5000)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
app.run(port=5000)
and then in the same directory run the command
然后在同一目录中运行命令
python run.app
回答by cph2117
You have to set the FLASK_APP variable outside of the virtual environment. I'm assuming the reason that it worked after @Fang restarted the command window is because that action automatically exited the virtual environment.
您必须在虚拟环境之外设置 FLASK_APP 变量。我假设它在@Fang 重新启动命令窗口后起作用的原因是该操作自动退出了虚拟环境。
So, if you're in the virtual environment (indicated by a (venv)
at the start of line on command window), then type the following commands:
因此,如果您在虚拟环境中((venv)
在命令窗口的行首用 a 表示),请键入以下命令:
deactivate
set FLASK_APP=run.py
venv\scripts\activate
flask run
回答by Sam Underwood
Did you run your terminal as administrator? I had the same problem, and running cmd/PowerShell as admin fixed it for me.
您是否以管理员身份运行终端?我遇到了同样的问题,以管理员身份运行 cmd/PowerShell 为我修复了它。
Edit: I spoke too soon. Running as admin did nothing.
编辑:我说得太早了。以管理员身份运行什么也没做。
The real answer (to my problem, at least) is a combination of some of the other answers.
真正的答案(至少对于我的问题)是其他一些答案的组合。
- Instead of using
set FLASK_APP = myApp.py
, usesetx FLASK_APP myApp.py
- Restart the terminal, and FLASK_APP will have the new value
- 而不是使用
set FLASK_APP = myApp.py
,使用setx FLASK_APP myApp.py
- 重启终端,FLASK_APP 就会有新的值
回答by Barry Zhai
Just drop the space around '=' !
只需删除 '=' 周围的空格!
After half an hour searching for solution, I finally get what does 'You did not provide the FLASK_APP environment variable' mean. Because what I set is 'FLASK_APP ' variable (with space).
经过半个小时的寻找解决方案,我终于明白“您没有提供 FLASK_APP 环境变量”是什么意思。因为我设置的是 'FLASK_APP ' 变量(带空格)。
回答by nguyên
Success with setx FLASK_APP test.py
. Note: if not run, close command line and start other one.
成功与setx FLASK_APP test.py
. 注意:如果没有运行,请关闭命令行并启动其他命令行。
D:\projects\flask-oauthlibpy2>.evn\Scripts\activate
(.evn) D:\projects\flask-oauthlibpy2>setx FLASK_APP test.py
SUCCESS: Specified value was saved.
(.evn) D:\projects\flask-oauthlibpy2>flask run
* Serving Flask app "flask-oauthlibpy2.test"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [24/May/2017 14:35:06] "GET / HTTP/1.1" 200 -
回答by Dhruv Ramani
Your code is correct. Try setting the Command-Line variable like this :
你的代码是正确的。尝试像这样设置命令行变量:
setx FLASK_APP run.py
And then run it : flask run
.
然后运行它:flask run
。
回答by Sagnik
Just drop the space around equal to sign. set FLASK_APP=python_file_name.py
只需将等号周围的空格删除即可。设置 FLASK_APP=python_file_name.py