【问题标题】:Error: While importing 'new_app', an ImportError was raised错误:导入“new_app”时,引发了 ImportError
【发布时间】:2021-12-24 14:23:42
【问题描述】:

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

  1. 创建目录并打开它
  2. 使用命令python -m venv env创建了一个虚拟环境
  3. 使用env\Scripts\Activate激活虚拟环境。
  4. 使用命令set FLASK_APP-new_app.py
  5. 尝试使用命令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.

谁能帮帮我。

【问题讨论】:

  • 能否将pip freeze 的输出添加到问题中?
  • 同样declarative_base 驻留在sqlalchemy.orm 中,因为版本 1.4:from sqlalchemy.orm import declarative_base
  • 导出 FLASK_APP=new_app.py 并使用 set FLASK_APP=new_app.py 代替 set FLASK_APP-new_app.py

标签: python postgresql flask sqlalchemy


【解决方案1】:

在 app.py 的结尾加上一行: app.run()

并调用 python app.py

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

【讨论】:

    【解决方案2】:

    从外观上看,你有一个简单的错字。

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

    试试看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2021-12-30
      • 2018-04-11
      • 2022-01-04
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多