Error: While importing 'new_app', an ImportError was raised

Error: While importing 'new_app', an ImportError was raised

我正在制作 Flask 应用程序,我遵循的步骤是

  1. 创建目录并打开
  2. 使用命令 python -m venv env
  3. 创建了一个虚拟环境
  4. 使用env\Scripts\Activate激活了虚拟环境。
  5. 使用命令set FLASK_APP-new_app.py
  6. 已尝试 运行 使用命令 flask run

当我尝试演示 helloworld 时,该应用程序 运行 很好,但对其进行了一些更改,如下所示

new_app.py

from flask import *
from flask_sqlalchemy import SQLAlchemy
import datetime
from datetime import datetime
import re
from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base
app = Flask(__name__)

app.config["SQLAlCHEMY_TRACK_NOTIFICATION"]=True
app.config["SQLAlCHEMY_TRACK_MODIFICATIONS"]=True

POSTGRES = {
     'user': 'postgres',
     'pw': '123',
     'db': 'ffs',
     'host': 'localhost',
     'port': '5431',
}
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(user)s:\
%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES
app.debug=True
db= SQLAlchemy(app)


class sms(db.Model):
    __tablename__='data_entry'
    id = Column(Integer, Sequence('data_entry_id_seq'), primary_key=True)
    latitude = db.Column(db.Float(),nullable=False)
    longitude = db.Column(db.Float(),nullable=False)
    date_of_fire = db.Column(db.DateTime(),nullable=False)
    fire_start_time = db.Column(db.Time(),nullable=False)
    def __init__(self,latitude,longitude,date_of_fire,fire_start_time):
        self.id
        self.latitude=latitude
        self.longitude=longitude
        self.date_of_fire=date_of_fire
        self.fire_start_time=fire_start_time



@app.route('/SMS',methods=['POST'])
def psms():
    smsdata=request.get_json()
    stringtemp=smsdata["smsString"]
    value=smsdata["smsString"].rfind("(")
    valueEnd=smsdata["smsString"].rfind(")")
    finalValue=smsdata["smsString"][value+1:valueEnd]
    last=finalValue.split(",")
    print(last)
    latitude=last[0]
    longitude=last[1].lstrip()    
    latitude = sum(float(x) / 60 ** n for n, x in enumerate(latitude[:-1].split(' ')))  * (1 if 'N' in latitude[-1] else -1)
    longitude = sum(float(x) / 60 ** n for n, x in enumerate(longitude[:-1].split(' ')))  * (1 if 'E' in longitude[-1] else -1)
    print(latitude,longitude)
    matchdate= re.search(r'\d{2}-\d{2}-\d{2}', stringtemp)
    matchtime= re.search(r'\d{2}:\d{2}:\d{2}', stringtemp)
    finaldate=matchdate.group()
    finaldate=datetime.strptime(finaldate,'%d-%m-%y').strftime("%Y-%m-%d %H:%M:%S")
    finaltime=matchtime.group()
    finaltime = datetime.strptime(finaltime,'%H:%M:%S').time()

    print(finaldate,finaltime)

    msg= sms(latitude=latitude,longitude=longitude,date_of_fire=finaldate,fire_start_time=finaltime)
    db.session.add(msg)
    db.session.commit()

    return jsonify(smsdata)

抛出了这样的错误

 * Serving Flask app 'new_app' (lazy loading)
 * 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: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.

Error: While importing 'new_app', an ImportError was raised.

谁能帮我解决这个问题。

从字面上看,您的错字很简单。

'set FLASK_APP-new_app.py' 应该是 'set FLASK_APP=new_app.py'.

试试看。

结束 app.py 行: app.run()

并调用 python app.py

将输出更多有用的诊断信息,如堆栈跟踪