flask run vs. python

2024/10/15 15:28:04

I'm having difficulty getting my flask app to run by using the "python" method. I have no problems using

export FLASK_APP=microblog.py
flask run

but attempting to use

python microblog.py

will result in the following error

ImportError: No module named 'app'

where my microblog.py file looks like this:

from app import app, db
from app.models import User@app.shell_context_processor
def make_shell_context():return {'db': db, 'User': User}

and my __init__.py file looks like this:

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migratefrom flask_login import LoginManagerapp = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)login = LoginManager(app)
login.login_view = 'login'from app import routesif __name__ == '__main__':app.run()

The folder these files reside in is called 'app'. I've tried putting the

if __name__ == '__main__':app.run()

In the microblog.py file as well, which was also unsuccessful.

I also have folders for templates/static/etc. and a routes file where I run all my @app.route() commands

Again, everything works fine when I run "flask run" in terminal, it just breaks down when I try using "python microblog.py". I'm trying to launch the app on AWS and just about every tutorial requires the applications to be called using the "python" method. Any help is appreciated.

Answer

[Solved]

I had my directories mixed up. I pulled microblog.py back outside of the app folder and was able to run python microblog.py with no issues and without having to edit anything.

In hindsight, I probably should have posted the file structure of everything right in the beginning.

https://en.xdnf.cn/q/69272.html

Related Q&A

Pandas-Add missing years in time series data with duplicate years

I have a dataset like this where data for some years are missing .County Year Pop 12 1999 1.1 12 2001 1.2 13 1999 1.0 13 2000 1.1I want something like County Year Pop 12 1999 1.1 12…

Saving zip list to csv in Python

How I can write below zip list to csv file in python?[{date: 2015/01/01 00:00, v: 96.5},{date: 2015/01/01 00:01, v: 97.0},{date: 2015/01/01 00:02, v: 93.75},{date: 2015/01/01 00:03, v: 96.0},{date: 20…

unable to download the pipeline provided by spark-nlp library

i am unable to use the predefined pipeline "recognize_entities_dl" provided by the spark-nlp library i tried installing different versions of pyspark and spark-nlp libraryimport sparknlp from…

Can __setattr__() can be defined in a class with __slots__?

Say I have a class which defines __slots__:class Foo(object):__slots__ = [x]def __init__(self, x=1):self.x = x# will the following work?def __setattr__(self, key, value):if key == x:object.__setattr__…

mysql-connector python IN operator stored as list

I am using mysql-connector with python and have a query like this:SELECT avg(downloadtime) FROM tb_npp where date(date) between %s and %s and host like %s",(s_date,e_date,"%" + dc + &quo…

Pandas: Use iterrows on Dataframe subset

What is the best way to do iterrows with a subset of a DataFrame?Lets take the following simple example:import pandas as pddf = pd.DataFrame({Product: list(AAAABBAA),Quantity: [5,2,5,10,1,5,2,3],Start…

Can I parameterize a pytest fixture with other fixtures?

I have a python test that uses a fixture for credentials (a tuple of userid and password)def test_something(credentials)(userid, password) = credentialsprint("Hello {0}, welcome to my test".f…

fit method in python sklearn

I am asking myself various questions about the fit method in sklearn.Question 1: when I do:from sklearn.decomposition import TruncatedSVD model = TruncatedSVD() svd_1 = model.fit(X1) svd_2 = model.fit(…

Django 1.9 JSONField update behavior

Ive recently updated to Django 1.9 and tried updating some of my model fields to use the built-in JSONField (Im using PostgreSQL 9.4.5). As I was trying to create and update my objects fields, I came a…

Using Tweepy to search for tweets with API 1.1

Ive been trying to get tweepy to search for a sring without success for the past 3 hours. I keep getting replied it should use api 1.1. I thought that was implemented... because I can post with tweepy.…