cyber_champ
cyber_champ

Reputation: 31

python flask import. import error unknown location

i am trying to import create_app from init.py that is located in the website file but everytime I try to run the code I get

ImportError: cannot import name 'create_app' from 'website' (unknown location)

this is my files

.vscode
env
website
   --static
   --templates
   --__init__.py
   --auth.py
   --views.py
   --models.py
main.py

init.py

from flask import Flask
def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'hello'
    return app

main.py

from website import create_app
app = create_app
if __name__ == '__main__':
    app.run(debug=True)

thought this is the only method that isn't working I tried this method to check if the error is from vscode but it is just from this method I tried app.py

from flask import Flask 
app = Flask(__name__)
@app.route("/")
def home():
    return "hello Flask"

and when I write in the terminal `python -m flask run I would get a website that says "hello Flask" but when I press the run icon I get nothing

unlike the first one if I run it I would get an import error unknown location and if I use python -m flask run I would get

Error: could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module not found in the current directory.

thought everything is sync to Github in both of them I am working in a 'script' environment

Upvotes: 3

Views: 8279

Answers (10)

The error was related to a bad located init file, it was in templates, while must be in folder, once I moved everything came Okey.

Upvotes: 0

Abdulazeez Salihu
Abdulazeez Salihu

Reputation: 11

Folder Structure:

.vscode/
env/
website/
   -- static/
   -- templates/
   -- __init__.py
   -- auth.py
   -- views.py
   -- models.py
main.py

Correct Import Statement:

from website.template import create_app

importing create_app function from website.template module solved this cause __init__.py is inside website/template/__init__.py

The import statement in main.py was:

from website import create_app

This statement suggests that create_app is directly inside the website package, but in your actual folder structure, it is inside the website.template module.

Upvotes: 0

I managed to Fix it.

It is simple you will be surprised, Just Go to you file init.py and run it first before you go run your main.py and run it.

I think the problem rises from the fact that both file after importing were not run. So after importing you must run all your files.

I hope it works.

Upvotes: 0

aaronm012
aaronm012

Reputation: 1

I found the fix.

Your __init__.py shouldn't be in the templates folder, move it out to the website folder.

Upvotes: 0

user21179911
user21179911

Reputation: 11

A fix I came up with was adding name to the function defining create_app inside the init file. Example:

from flask import Flask

def create_app(name): app = Flask(name)

Upvotes: 1

Kohane
Kohane

Reputation: 1

try this in your main.py

from website.init import create_app

or change file init to init.py then in your main.py

from website._init_ import create_app

Upvotes: 0

DhaniPro
DhaniPro

Reputation: 59

You forgot create environment variable. if you using mac or linux export FLASK_APP=main.py if you windows set FLASK_APP=main.py

And last in main.py should be app = create_app()

Upvotes: 0

icxcNoname
icxcNoname

Reputation: 1

For someone getting this error later (like myself), be sure to check that your main package's

__init__.py

file is in the correct place.

Upvotes: 0

Ahmed Hammad
Ahmed Hammad

Reputation: 1

I ran into the same thing, but it appears that these files "models, views, etc" isn't in website folder, they are inside static folder which is in website folder, that's why it's not working. I just fixed them manually.

Upvotes: 0

When importing, try from website.templates import create_app. So basically after you put it to import from website, add . following the sub-folder that you desired to import from (in this case it is templates).

Upvotes: 0

Related Questions