简体   繁体   中英

flask tutorial troubleshooting: ModuleNotFoundError: No module named 'app'

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM