简体   繁体   English

错误:无法在 VSCode 中找到 Flask 应用程序

[英]Error: Could not locate a Flask application in VSCode

I am trying to learn Flask using VScode.我正在尝试使用 VScode 学习Flask

The tutorial that I am following is: Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started .我关注的教程是: Python Flask 教程:全功能 Web 应用程序第 1 部分 - 入门

I did the following things:我做了以下事情:

  1. Created a new virtualenv in a folder using: virtualenv venv使用以下命令在文件夹中创建了一个新的 virtualenv: virtualenv venv
  2. activated it as: venv\Scripts\activate (I am on Windows 10)将其激活为: venv\Scripts\activate (我在 Windows 10 上)

After that, I created a new directory named Flask_Blog using mkdir Flask_Blog and in it, I created a new flaskblog.py file containing the following code:之后,我使用mkdir Flask_Blog创建了一个名为Flask_Blog的新目录,并在其中创建了一个包含以下代码的新flaskblog.py文件:

from flask import Flask


app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello'

Then, in the terminal of VScode , I changed my working directory in order to be in the Flask_Blog directory using cd Flask_Blog .然后,在VScode的终端中,我更改了我的工作目录,以便使用cd Flask_Blog进入Flask_Blog目录。

Now, when I am doing set FLASK_APP=flaskblog.py followed by flask run , I am getting the following error:现在,当我执行set FLASK_APP=flaskblog.py后跟flask run时,出现以下错误:

(venv) PS C:\Users\kashy\OneDrive\Desktop\Flask\Flask_Blog> flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

But

When I do the same in the cmd prompt, the code runs and I get to see the output.当我在cmd提示符下执行相同操作时,代码运行并且我可以看到输出。

I am completely new to this.我对此完全陌生。 Can anyone please tell me what is the mistake I am doing in VSCode and why is it working in the cmd ?谁能告诉我我在VSCode中犯了什么错误,为什么它在cmd中工作?

Issue raised in VsCode VsCode 中提出的问题

Under Powershell, you have to set the FLASK_APP environment variable as follows:在 Powershell 下,您必须按如下方式设置 FLASK_APP 环境变量:

$env:FLASK_APP = "webapp"

Then you should be able to run "python -m flask run" inside the hello_app folder.然后你应该能够在 hello_app 文件夹中运行“python -m flask run”。 In other words, PowerShell manages environment variables differently, so the standard command-line "set FLASK_APP=webapp" won't work.换句话说,PowerShell 以不同的方式管理环境变量,因此标准命令行“set FLASK_APP=webapp”将不起作用。

Try Set FLASK_APP = Full path of the folder/filename.py.尝试设置 FLASK_APP = 文件夹/文件名.py 的完整路径。

This worked for me这对我有用

I am trying to learn Flask using VScode.我正在尝试使用 VScode 学习Flask

The tutorial that I am following is: Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started .我正在关注的教程是: Python Flask 教程:全功能 Web 应用程序第 1 部分 - 入门

I did the following things:我做了以下事情:

  1. Created a new virtualenv in a folder using: virtualenv venv使用: virtualenv venv在文件夹中创建了一个新的 virtualenv
  2. activated it as: venv\Scripts\activate (I am on Windows 10)将其激活为: venv\Scripts\activate (我在 Windows 10 上)

After that, I created a new directory named Flask_Blog using mkdir Flask_Blog and in it, I created a new flaskblog.py file containing the following code:之后,我使用mkdir Flask_Blog创建了一个名为Flask_Blog的新目录,并在其中创建了一个包含以下代码的新flaskblog.py文件:

from flask import Flask


app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello'

Then, in the terminal of VScode , I changed my working directory in order to be in the Flask_Blog directory using cd Flask_Blog .然后,在VScode的终端中,我更改了我的工作目录,以便使用cd Flask_Blog进入Flask_Blog目录。

Now, when I am doing set FLASK_APP=flaskblog.py followed by flask run , I am getting the following error:现在,当我执行set FLASK_APP=flaskblog.py后跟flask run时,我收到以下错误:

(venv) PS C:\Users\kashy\OneDrive\Desktop\Flask\Flask_Blog> flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

But

When I do the same in the cmd prompt, the code runs and I get to see the output.当我在cmd提示符中执行相同操作时,代码运行,我看到 output。

I am completely new to this.我对此完全陌生。 Can anyone please tell me what is the mistake I am doing in VSCode and why is it working in the cmd ?谁能告诉我我在VSCode中犯了什么错误,为什么它在cmd中工作?

This worked for me on the VSCode:这在 VSCode 上对我有用:

$env:FLASK_APP= 'C:\Python\ex003\main:app' $env:FLASK_APP= 'C:\Python\ex003\main:app'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM