59

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
codegeek
  • 32,236
  • 12
  • 63
  • 63
onepiece
  • 3,279
  • 8
  • 44
  • 63

13 Answers13

49

__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

Elisha
  • 4,811
  • 4
  • 30
  • 46
  • 6
    I renamed the directory to 'app', still getting the same error – onepiece Mar 28 '14 at 11:56
  • 2
    don't rename, add a new one and put this file there. (the problem is because `run.py` is also inside the directory) – Elisha Mar 28 '14 at 12:17
  • but I think that what you really want is to rename the file name to `app.py` – Elisha Mar 28 '14 at 12:18
  • 1
    Could you clarify why I need to make a new folder instead of just renaming the current one? – onepiece Mar 28 '14 at 12:22
  • 3
    it is better to rename. `__init__.py` is meant for cases that you want a directory with several files to be a one package. – Elisha Mar 28 '14 at 12:30
  • 1
    i actually just removed run.py from app folder and placed it outside and run it. It worked but i wonder how it worked? – ashim888 Dec 04 '14 at 06:25
  • I renamed it it still getting same error – Software Engineer Feb 06 '23 at 13:58
  • @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 yours – Elisha Feb 19 '23 at 09:12
32

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)

d-_-b
  • 761
  • 2
  • 11
  • 26
18

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!"
Robert Brisita
  • 5,461
  • 3
  • 36
  • 35
Nuhman
  • 1,172
  • 15
  • 22
  • 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".` – user5359531 Sep 04 '18 at 19:47
  • @user5359531 either `export FLASK_APP=main` or `export FLASK_APP=main.py` worked for me with this structure. – andrei1111 Jun 08 '20 at 17:27
  • 1
    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. – Wok Jan 18 '21 at 15:25
  • 1
    This is the solution for me. I kept the run.py inside app folder. Changing its directory solved it. Thanks. – Sadman Amin Apr 13 '21 at 06:45
  • How do you actually run this app and from what path? `run.py ` – Pavel Fedotov Jan 19 '22 at 12:19
9

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

ckjavi70
  • 168
  • 3
  • 10
7

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.

B.Mr.W.
  • 18,910
  • 35
  • 114
  • 178
Takeshi Patterson
  • 1,207
  • 6
  • 15
  • 29
4

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

joepadz
  • 91
  • 1
2

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

2

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!

brianj
  • 51
  • 3
1

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

  • 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....' – m_h Jan 24 '23 at 11:49
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.

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
  • I had a similar issue: ```python -m FLASK_APP='wsgi_app.py' flask shell ``` -- Separating the environment variable worked for me! Thank you! – baumannalexj Aug 07 '21 at 04:11
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

A B
  • 189
  • 2
  • 9
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)

BEWARB
  • 131
  • 1
  • 10
0

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"}
Kit Law
  • 323
  • 6
  • 15