Flask - ImportError: No module named app
Asked Answered
L

13

67

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
Lout answered 28/3, 2014 at 11:5 Comment(1)
I had to place the run.py outside of the app directory to fix thisIlltimed
G
51

__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

Gruesome answered 28/3, 2014 at 11:33 Comment(8)
I renamed the directory to 'app', still getting the same errorLout
don't rename, add a new one and put this file there. (the problem is because run.py is also inside the directory)Gruesome
but I think that what you really want is to rename the file name to app.pyGruesome
Could you clarify why I need to make a new folder instead of just renaming the current one?Lout
it is better to rename. __init__.py is meant for cases that you want a directory with several files to be a one package.Gruesome
i actually just removed run.py from app folder and placed it outside and run it. It worked but i wonder how it worked?Inertia
I renamed it it still getting same errorJesus
@SoftwareEngineer so you are probably doing something else wrong, difficult to help with not enough context, what you files structures? how do you run it? maybe worth creating a new question if this one didn't answers yoursGruesome
U
40

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)

Unrighteous answered 19/8, 2021 at 16:55 Comment(6)
Thank you. Indeed was not that intuitive, using PyCharm.Declamatory
Thank you! I also noticed that running app from comand line also fixed the issue: FLASK_APP=app.py flask runTransform
it took me an eternity to find the solution, thank youTrudey
Saved my day... deepest gratitudes, thank you for sharing, brother.Endicott
You saved my day! Any alternatives that I can use debug=True when using VSCode debug mode?Woodenware
setting debug=True seems pointless as I can step thru my code with it set to false, but Flask crashes immediately if set to true.Judgeship
L
20

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!"
Leon answered 13/4, 2018 at 11:52 Comment(5)
How do you run app from the command line in this case? The docs suggest export FLASK_APP="movies/app"; run flask but it does not work; flask.cli.NoAppException: Could not import "app".Rossie
@Rossie either export FLASK_APP=main or export FLASK_APP=main.py worked for me with this structure.Winder
This helps a lot. Somehow, I was trying to do this with an empty __init__.py, and what you have put in this file in an app.py file, and it was not working. I think I was missing app.run() too.Artificer
This is the solution for me. I kept the run.py inside app folder. Changing its directory solved it. Thanks.Gainor
How do you actually run this app and from what path? run.py Temptation
S
11

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

Socinian answered 4/4, 2019 at 19:54 Comment(2)
Thanks, yes, I'm using flask boilerplate and this what actually helped export PYTHONPATH="$PYTHONPATH:/var/gx/app"Manolete
in vscode you can put export PYTHONPATH="$PYTHONPATH:/path/to/your/src" to launch.jsonSwitchblade
H
8

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.

Historicism answered 5/7, 2014 at 19:49 Comment(0)
D
5

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

Distaste answered 12/7, 2020 at 12:7 Comment(0)
T
5

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

Turgite answered 23/7, 2020 at 11:46 Comment(0)
H
3

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!

Hibbert answered 13/5, 2022 at 1:40 Comment(0)
T
1

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

Thibeault answered 2/1, 2023 at 21:54 Comment(1)
Indeed. Go to parent folder and call it with e.g. app.run.. If someone is getting this error in FastAPI with Uvicorn, simply run e.g. 'uvicorn app.main:app --host .... -- port....'Bunnybunow
W
1

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"}
Woodenware answered 5/7, 2023 at 9:30 Comment(0)
I
0

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.

Italy answered 30/8, 2020 at 11:9 Comment(1)
I had a similar issue: python -m FLASK_APP='wsgi_app.py' flask shell -- Separating the environment variable worked for me! Thank you!Knish
V
0

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

V answered 26/1, 2023 at 4:17 Comment(0)
A
0

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)

Antagonize answered 2/7, 2023 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.