-1

Here init.py has a function create_app ans I am not able to import the function in main.py.

I've attached main.py and init.py [enter image description here](https://i.stack.imgur.com/E9YMh.png)

It keeps on giving ImportError: cannot import name 'create_app' from 'website' (unknown location)

I have tried importing init but it isn't working either.

  • 1
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Marcelo Paco Apr 15 '23 at 21:56
  • 1
    Please read [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/q/285551/354577). Instead, format code as a [code block]. The easiest way to do this is to paste the code as text directly into your question, then select it and click the code block button. – ChrisGPT was on strike Apr 17 '23 at 01:20

4 Answers4

0

The error is occurring because of the from ... import ... statement, because you are trying to import a folder. To import a file in that folder, use import website.create_app, though this will also cause an error because there is no file named create_app.py. The correct code would be:

import website.create_app
app = create_app

if __name__ == "__main__":
  app.run(debug=True)
0

From what I see in the picture, I am assuming this to be the directory structure:

Flask-web-app
├── say-hello
├── website
│   ├── static/
│   ├── templates/
│   ├── __init__.py
│   ├── auth.py
│   ├── models.py
│   └── views.py
└── main.py

Your main.py should contain:

from website import views    #filename should be used here

app = views.app    #considering views.py has an app variable defined

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

The issue was in the way you were using relative imports.

Shubham Garg
  • 459
  • 3
  • 10
  • the function(create_app) is defined in __init__.py file but the tutorial says it will make website folder into a package so we can import it this way. – Prince mehta Apr 15 '23 at 22:29
0

I think you should definitely try to write if __name__ == "__main__":in your__init__.py. (This will allows the script to be run as a standalone program)

And then you can try to import the function create_app in your main.py from __init__.py by using import create_app from __init__.py. I think the problem here is instead of using import create_app from website you should try to directly import it from your __init__.py

Lastly you can also try to import all of the function from your __init__.py by using from__init__.py import * enter image description here. Hope this could help you!

Zide Yang
  • 1
  • 1
0

The Init.py file is in the templates folder as your image shows.

You have to import it like this:

from website.templates import create_app
Deadlooks
  • 132
  • 10