iglu
iglu

Reputation: 11

Cannot import app from app (Unknown Location) Flask

Terminal Dump -

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.

Error: While importing 'microblog', an ImportError was raised:

Traceback (most recent call last):
  File "/home/iglu/repos/personal/Flask Mega Tutorial/microblog/venv/lib/python3.9/site-packages/flask/cli.py", line 256, in locate_app
    __import__(module_name)
  File "/home/iglu/repos/personal/Flask Mega Tutorial/microblog/microblog.py", line 1, in <module>
    from app import app
ImportError: cannot import name 'app' from 'app' (unknown location)

File Structure-

├── app
│   ├──  __init__.py
│   ├── __pycache__
│   │   └── microblog.cpython-39.pyc
│   └── routes.py
├── microblog.py
├── __pycache__
│   └── microblog.cpython-39.pyc
├── README.md
└── venv

init.py

from flask import Flask

app = Flask(__name__)

from app import routes

routes.py

from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"

microblog.py

from app import app

The python variable is indeed set to the venv one image

Python 3.9.5 Flask 2.0.1 Werkzeug 2.0.1

Any help would be great! I have tried my best to follow the guide precisely.

Upvotes: 0

Views: 541

Answers (2)

iglu
iglu

Reputation: 11

Fixed it, it was a typo it was just a typo in the init.py file name.

Cannot close so uhh consider it closed

Upvotes: 0

P0intMaN
P0intMaN

Reputation: 134

Based on your file structure, python is in no way going to know that microblog.py exists. That's because, when you type from app import app, python will look for the package app

Now, your microblog.py is not a part of the package app. So, try moving it inside the app folder (more precisely a package because any folder having init is called a package) and it will work fine.

Upvotes: 1

Related Questions