Flask - Importerror: No Module Named App

First I created __init__.py

from flask import Flask

app = Flask(__name__)

Then in a separate file, in the same directory, run.py

from app import app 

app.run(
    debug = True
)

When I try to run run.py, I get the error

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from app import app 
ImportError: No module named app
0

9 Answers

__init__.py is imported using a directory. if you want to import it as app you should put __init__.py file in directory named app

a better option is just to rename __init__.py to app.py

6

This is probably an error in flask application's folder structure.
Anyone looking for a simple beginner-friendly structure for the flask project may find this helpful:

   |__movies 
     |__run.py 
     |__app     
        ├── templates
        │   └── index.html
        │   └── signup.html
        └── __init__.py
        └── routes.py

Here 'movies' is the name given for the main application. It contains 'run.py' and a folder called 'app'. 'app' folder contains all necessary flask files such as 'templates' folder, '__init __.py', and 'routes.py'.

Contents of:

run.py:

from app import app

__init__.py:

from flask import Flask

app = Flask(__name__)

from app import routes


app.run(debug=True)

routes.py:

from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"
5

Your __init__.py file needs to go in the folder named app, not the same directory as the run.py file.

from app import app is looking in the app folder, so the __init__.py file needs to sit in there.

Ensure to set your PYTHONPATH to the src/ directory as well. Example export PYTHONPATH="$PYTHONPATH:/path/to/your/src"

2

Just rename your file to app.py and it will works.

(In case someone else is lost even after trying other solutions..)

I get the No module named app error only when Debugging, not when Running.

That's because I had set debug=True in my app.py's __main__ (which kindly auto-reloads flask when we save code changes) :

app.run(debug=True)

But if you try to Debug in your IDE now, it gives above error.

Fix : set debug=False

app.run(debug=False)

and it works like a magic trick illusion.

1

For me, export PYTHONPATH=/path/to/your/src && python app/main.py works

I solved this as follows -

$export FLASK_APP=run

$flask run

After executing this command. I don't get any error, my app running smoothly.

1

This may be an isolated case, but for me this was a VS Code issue. The "no module found" error would only happen when debug=True.

In VS Code, you can "Run Python File" or "Debug Python File". I was using debug in VS Code AND I had app.run(debug=True). Once I just ran the file normally in VS Code, the problem went away and Flask debugger is working as expected.

I guess it doesn't like 2 layers of Inception!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest