Flask – ImportError: No module named app

Question:

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
Asked By: onepiece

||

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

Answered By: Elisha

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.

Answered By: Takeshi Patterson

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!"
Answered By: Nuhman

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

Answered By: ckjavi70

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

Answered By: joepadz

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

Answered By: Jianping Ding

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.

Answered By: Parag Kulkarni

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

I get the No module named app error only during Debugging, not Running, in my IDE (VSCode)

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

app.run(debug=True)

How to fix – set debug=False in above :

app.run(debug=False)

and error goes away.

Answered By: d-_-b

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!

Answered By: brianj

you are probably running from inside your app folder. Move out to the previous directory and run the command again.

Answered By: amarocsjunior

I just want to leave this solution for whom other solutions aren’t working.

Assuming here the module "app" is actually referring to your "app.py" source file, in the app.run() ensure to set debug to FALSE i.e. app.run(debug=False). Otherwise cd to the folder in which your app.py file is and then run python app.py

These are workarounds, but it seems there is a bug in the debug=True flow as old as 2016-17, perhaps it hasn’t been fixed yet

Answered By: A B
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.