-1

I had the following issue with my flask application. I have the following file structure as follows. It seems the internal file include is not working. Can anyone please help me to sort out the issue. I am bit new to python and flask. app.py, db.py, blacklist.py all are only in the root directory. =====app.py=====

import flask
from flask_restful import Api
from flask_jwt_extended import JWTManager

from db import db
from blacklist import BLACKLIST

app = flask.Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET'])
def home():
    return "<h1>Api Testing</h1><p>This is just testing endpoint.</p>"

app.run()

====db.py====

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

====blacklist.py====

BLACKLIST = set()
flask.cli.NoAppException
flask.cli.NoAppException: While importing "xxxxxxx.app", an ImportError was raised: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app __import__(module_name) File "/Users/xxxxx/Documents/Vhosts/xxxxx/app.py", line 5, in <module> from db import db ModuleNotFoundError: No module named 'db'
Ruchira Chamara
  • 345
  • 2
  • 7
  • 20

3 Answers3

0

First off, you should wrap you app.run() call inside a main block, otherwise the server will try to start even if you are only importing the object (which is what the Flask CLI does with you call flask run). The reasoning behind this has been covered extensively elsewhere

import flask
from flask_restful import Api
from flask_jwt_extended import JWTManager

from db import db
from blacklist import BLACKLIST

app = flask.Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET'])
def home():
    return "<h1>Api Testing</h1><p>This is just testing endpoint.</p>"

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

That may solve your issue, however you should update your post to include the contents of your other files db.py and blacklist.py too. In your error message there is a mention of the db module, and without that there is nothing else to offer.

vulpxn
  • 731
  • 1
  • 6
  • 14
  • Thanks for the comment. I have updated my post as per your request. But still I am getting above error after updating. – Ruchira Chamara Apr 13 '20 at 03:51
  • I would cosign the advice of @dave-w-smith and make your module names more distinctive, there's a chance that Flask thinks that `db` is referring to something other than your file. Try changing the name and trying again. – vulpxn Apr 13 '20 at 03:56
  • it worked when I run the application like the following. /usr/local/opt/python/bin/python3.7 /Users/xxxx/Documents/Vhosts/xxxxx/app.py – Ruchira Chamara Apr 13 '20 at 04:41
  • It goes without saying that you should be inside of your working directory when trying to call a package (by providing an absolute path you are accomplishing the same thing) so if you were not inside the `xxxxx` folder before, that's where your problem was coming from. – vulpxn Apr 13 '20 at 04:45
  • Thanks for the explanation. Yes finally understood that. @vulpxn Thanks. – Ruchira Chamara Apr 13 '20 at 06:29
0

When you're getting started, you can avoid a lot of problems by keeping your names distinct. In this case, instead of

from db import db

rename your file, and do something like

from mydb import db

Then, once you have something working, take things step-by-step, noting where things stop working, and use that an an excuse to explore how Python handles namespaces.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
0

I solve this problem using terminal as follows :

$export FLASK_APP=app
$flask run

Due to .py extension this problem was occurred.

David Buck
  • 3,752
  • 35
  • 31
  • 35