51

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
  • 30,235
  • 11
  • 59
  • 61
onepiece
  • 2,999
  • 6
  • 40
  • 58

9 Answers9

46

__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,513
  • 4
  • 27
  • 45
  • 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
17

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
  • 4,684
  • 2
  • 33
  • 33
Nuhman
  • 1,081
  • 16
  • 20
  • 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. – andrei13 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
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.
  • 17,766
  • 32
  • 107
  • 160
Takeshi Patterson
  • 1,055
  • 6
  • 14
  • 29
6

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

ckjavi70
  • 138
  • 2
  • 10
3

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

joepadz
  • 81
  • 1
2

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

2

(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.

d-_-b
  • 433
  • 1
  • 7
  • 22
1

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
  • 35,183
  • 9
  • 62
  • 80
  • 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

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
  • 31
  • 1