Flask, django-how to import and run the project in pycharm, when flask appears, you did not provide the "FLASK_APP" error resolution

     Note: Generally there are two ways to run flask,

One is to specify run directly in the code, for example:

from flask import Flask
app = Flask(__name__)
 
# 首页
@app.route('/')  # 接口地址
def index():
    # 接口本身
    return 'home'
 
# 登录页
@app.route('/login')  #接口地址
def login():
    # 接口本身
    return 'login sucess'
 
# web 服务器
if __name__ == '__main__':
    app.run()  #python *.py即可启动服务
 
# 重新运行程序,输入网址显示如下图

The second is to start the service through instructions, as follows:

# 5. 本地启动服务
$ FLASK_ENV=development FLASK_APP=apps.manage flask run --host 0.0.0.0 --port 8080 

In fact, the second method is more applicable, and it can be used in multi-core startup methods, such as the command:

#!/usr/bin/env bash

# 默认启动4个worker,云部署的时候可添加环境变量来更改
if [ ! "$WORKERS" ]; then
  WORKERS=4
fi
gunicorn apps.manage:app --log-level=info --workers=$WORKERS --timeout=600 --worker-class=gevent --bind=0.0.0.0:8080

The following pycharm configuration is also used for the second type. If it is the first type, you don't need the following configuration, just start the python code directly.

 

1. Import the flask project

File-Open selection needs to import the flask project (for example, the project name is flasky).

 2. Set the interpreter

File-Settings-Project Interpreter, as shown in the figure below

3. Add Flask server

1. Click Add Configuration in the upper right corner of pycharm

2. Click the + sign and select Flask Server

3. Fill in the parameters

4. Specify the development port for the flask service:

5. Run the project

Click the run button in the upper right corner of pycharm:

Enter the access address in the browser: http://127.0.0.1:5000/

4. You did not provide the "FLASK_APP" error resolution:

The main reason is: the web service configuration 3. The python interpreter added when adding Flask server did not load the corresponding module in the setting of pycharm, which resulted in the report that FLASK_APP was not set when the flask service was started.

The error is as follows:

“ You did not provide the "FLASK_APP" environment variable”

Solution:

This problem can be solved perfectly after loading the corresponding python interpreter.

V. Version description

1.python:python3

2. pycharm: PyCharm 2018.2 (Professional Edition)
If the version is different, then the result of following the steps above may be different.

 

6. Django configures and runs the project in pycharm:

pycharm runs the settings.py file, which is the main configuration file to start the django project , and its configuration content is as follows:

Note: The second step may or may not be added by yourself. If it is not automatically added, pycharm will usually report an error message, and then specify the settings.py file in the pop-up dialog box . The main ones are in the settings.py file:

WSGI_APPLICATION = 'mighty_shier.wsgi.application'

 

 

 

Guess you like

Origin blog.csdn.net/yangdashi888/article/details/106762116
Recommended