flask tutorial troubleshooting: ModuleNotFoundError: No module named 'app'
your run.py is not able to import app as it can not see app within the bin folder, what happens with python is that all python files are treated as modules and the folders with an init.py file are treated as packages so run.py will start looking for the app package to import the app module however it will search within the bin directory. Read through Python documentation to fully understand the modules and packages. for now you might want to reorganize your application directory to look like this:
dir app
file app.py
dir flask
file run.py
By ensuring that run.py and app directory are at the same level in the directory run.py will be able to import from app now.
I hope that helps
Cora Coleman
Updated on June 05, 2022Comments
-
Cora Coleman almost 2 years
I am having trouble running my run.py file. My file structure looks like this:
With another python file called 'run.py' located in flask/bin along with python3. My run.py file is simply:
#!flask/bin/python3 from app import app app.run(debug=True)
However running 'python3 run.py' throws the error:
$ python3 run.py Traceback (most recent call last): File "run.py", line 2, in <module> from app import app ModuleNotFoundError: No module named 'app'
app.py looks like:
from flask import Flask app = Flask(__name__) from app import views
I am confused about how to solve this as I have been messing with the directories such as putting app.py into the flask/bin folder and putting it outside of all folders shown in my directory above, but these methods have not worked for me.
-
Cora Coleman about 7 yearsThank you very much you are right and it is working, turns out the tutorial I am doing stated to put in into the root folder! I was confused bc I didn't understand the concept of a shebang until now and tried putting it where the shebang directed haha
-
Achim Munene about 7 yearsam always happy to assist i have a basic flask tutorial series on my blog you could check it out i explained some of the stuff there