-1

I am learning flask from https://blog.miguelgrinberg.com/

I have application microblog with filename as microblog.py

from app import app

and I have directory named app and it contains __init__.py with below code

from flask import Flask
from config import Config

app = Flask( __name__ )
app.config.from_object(Config)
from app import routes

But when I run flask run I am getting error as

ImportError: No module named 'app'

I understand like if I want to indicate a directory as a package then I have to include a __init__.py inside the directory and I did so for app directory.

Directory structure

.
├── __init__.py
├── app
│   ├── __init__.py
│   ├── forms.py
│   ├── routes.py
│   └── templates
│       ├── base.html
│       ├── index.html
│       └── login.html
├── config.py
└── microblog.py

Full stacktrace


flask run                                       
 * Serving Flask app "microblog.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "microblog.microblog", an ImportError was raised:

Traceback (most recent call last):
  File "~/anaconda3/envs/flask_python3.5.2/lib/python3.5/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "~/learning/migual_flask/microblog/microblog.py", line 1, in <module>
    from app import app
ImportError: No module named 'app'

And this code worked till recently and suddenly started giving troubles. I am unable to find where this is going wrong.

Any help greatly appreciated.

Thanks.

Raja G
  • 5,973
  • 14
  • 49
  • 82
  • Can you show your directory hierarchy? i.e. where are the files and where are you calling from? – knh190 May 17 '19 at 04:22
  • Why do you have the last line `from app import routes%`? What are you trying to do? – Kes115 May 17 '19 at 04:37
  • @knh190, I have updated post with directory structure and that % symbol came as part of the `cat` command output. – Raja G May 17 '19 at 06:15
  • Can you post full stacktrace? – Tech at The Sparks Foundation May 17 '19 at 06:29
  • Maybe it was caused by your directory hierarchy. Try the following link. https://stackoverflow.com/questions/22711087/flask-importerror-no-module-named-app and why do you use this?: > from app import routes% Hope this can help you. Have a nice day. :D – ALFAFA May 17 '19 at 05:29
  • @Kasrâmvd, I have tried all I can but still facing the issue. I am able to import `app` module locally but while importing via `flask run` I am getting the error. Could you please help. – Raja G May 19 '19 at 15:18

1 Answers1

3

Okay, from what you post with the hierarchy, it's clear that it should work, but I suspect that you were importing from wrong directory.

Given the directory:

.
├── __init__.py
├── app
│   ├── __init__.py
│   ├── forms.py
│   ├── routes.py
│   └── templates
│       ├── base.html
│       ├── index.html
│       └── login.html
├── config.py
└── microblog.py

Assume you import them from root directory (the "." at the top):

$ls                                                                                                                                                        
__init__.py  app          config.py    microblog.py

$ls app
__init__.py  forms.py     routes.py

# which works
>>> import config
>>> import microblog
>>> import app

# and
>>> from app import routes
>>> from app import forms

I noticed that you have app = Flask() in your app/__init__.py, which is a very bad practice which should be avoided as much as possible (what makes you think it's good? I can't think of any), but it works for me:

>>> import app
>>> from app import app

# the first app is module
# the second is app = Flask()
# but the second one will overwrite the first one's name
# which is another terrible practice

And you can inspect a module:

>>> import app
>>> help(app) # app = Flask() is there
knh190
  • 2,744
  • 1
  • 16
  • 30
  • I am able to import the module via python console but via `flask run` still facing the same issue. – Raja G May 19 '19 at 15:20
  • @rɑːdʒɑ Your full traceback shows you are importing a module from a class in `microblog` but you are not showing the source code. – knh190 May 19 '19 at 15:41
  • I have uploaded source code at https://www.sendspace.com/file/nmic8r, Please check. – Raja G May 20 '19 at 06:12
  • And Instead of giving `flask run` I have given `python microblog.py` and its working now. I have defined a `.flaskenv` file with app name env variable. – Raja G May 20 '19 at 06:30
  • @rɑːdʒɑ Your zip doesn't match your traceback at all! What are you trying to do? I'm completely lost. But since my demo has clearly shown how you can make the import work, I'll close this followup. – knh190 May 20 '19 at 09:51
  • Issue solved, I have removed `__init__.py` file in the parent directory(microblog) and issue resolved. – Raja G May 21 '19 at 11:33