Home > Net >  Error: While importing 'new_app', an ImportError was raised
Error: While importing 'new_app', an ImportError was raised

Time:11-16

I am making flask app, the steps which i followed are

  1. made a directory and opened it
  2. made a virtual env using the command python -m venv env
  3. activated the virtual environment using env\Scripts\Activate.
  4. used command set FLASK_APP-new_app.py
  5. tried running using command flask run

the app is running well when i tried the demo helloworld, but made some changes in it which goes like this

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)

An error was thrown which is like this

 * 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.

Can someone help me out with this.

CodePudding user response:

You have a simple typo, from the looks of it.

'set FLASK_APP-new_app.py' should be 'set FLASK_APP=new_app.py'.

Try that.

  • Related