onepiece
onepiece

Reputation: 3529

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

Upvotes: 68

Views: 232800

Answers (13)

Kit Law
Kit Law

Reputation: 372

If you are having a project structure similar to

   |project1
     |README.md
     |project1     
        ├── assets
        │   └── header.png
        └── __init__.py
        └── app.py

and

If you are using VSCode as your IDE, and errors occur only during Debugging, not Running

Summarizing the answers from d-_-b and ckjavi70, 2 solutions are available.

Either in app.py, if __name__ == "__main__": , set

app.run(debug=False)

or in.vscode/launch.json, add the following line as part of configurations.

"program": "project1/app.py",
"env": {"PYTHONPATH": "${workspaceFolder}/project1"}

Upvotes: 1

BEWARB
BEWARB

Reputation: 131

for me, the issue was with pycharm IDE, we have set app and the directory containing app as source directory (by right click on directory>Mark directory as>Source Roots)

Upvotes: 0

d-_-b
d-_-b

Reputation: 883

in case you're still stuck..

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 (which auto-reloads flask after code changes) in app.py's __main__ :

app.run(debug=True)

To fix the error, just set it to False :

app.run(debug=False)

Upvotes: 40

A B
A B

Reputation: 191

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

Upvotes: 0

amarocsjunior
amarocsjunior

Reputation: 11

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

Upvotes: 1

brianj
brianj

Reputation: 69

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!

Upvotes: 3

Parag Kulkarni
Parag Kulkarni

Reputation: 9

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.

Upvotes: 0

Jianping Ding
Jianping Ding

Reputation: 51

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

Upvotes: 5

joepadz
joepadz

Reputation: 101

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

Upvotes: 5

ckjavi70
ckjavi70

Reputation: 308

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

Upvotes: 12

Nuhman
Nuhman

Reputation: 1270

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!"

Upvotes: 20

Takeshi Patterson
Takeshi Patterson

Reputation: 1277

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.

Upvotes: 8

Elisha
Elisha

Reputation: 4951

__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

Upvotes: 52

Related Questions