Flask cannot import from website create_app

Question:

ImportError: cannot import name 'create_app' from 'website'
from website import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)
Asked By: Liitmik

||

Answers:

whether you want a file that can be run as the main program or imported by other modules. We can use an

if __name__ == "__main__" block

to allow or prevent parts of code from being run when the modules are imported.

When the Python interpreter reads a file, the name variable is set as main if the module being run, or as the module’s name if it is imported. Reading the file executes all top level code, but not functions and classes (since they will only get imported).

Sample code of flask

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World!"
    
if __name__ == "__main__":
    app.run(debug=True)

I believe your the fault is instead of name == "main", you put name == main

Answered By: Sivaram Rasathurai

you should put init.py under website folder, not inside any folder else like static or templates folders

like this

enter image description here

Answered By: Almadani

Make sure you run your __init__.py file,
Then run the main.py file
Hope it helps:)

Answered By: shriya
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.