-1

The Following is my code hierarchy.

enter image description here

I am trying to create a package for my project, "UIGenerator" but it is giving me following error.

traceback (most recent call last):
  File "run.py", line 1, in <module>
    from UIGenerator import app
  File "/Users/______/Desktop/UIGenerator/UIGenerator/__init__.py", line 2, in <module>
    from site.routes import site
ModuleNotFoundError: No module named 'site.routes'; 'site' is not a package

Here is the code for run.py =>

from UIGenerator import app

@app.route('/')
def test_connection():
    return "<h1>Connected<h1>"

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

There is nothing in site =>__init__.py file and in site=>routes.py, I have some sample code to test-

from flask import Blueprint

site = Blueprint('site', __name__)

@site.route('/site')
def site():
    return "this is test for site package"

The following is my code for the UIGenerator's init.py file =>

from flask import Flask
from site.routes import site

def getApp():
    app = Flask(__name__)
    app.register_blueprint(site)
    return app

app = getApp()

Can anyone give me any clue, please.

Natasha
  • 6,651
  • 3
  • 36
  • 58

2 Answers2

1

One way to fix this is to build a Python package with you files, like outlined here. This will be better in the long run if you want to scale this project up, and will allow you to declare things in init

That being said, I think you can get your setup working by moving this chunk of code

def getApp():
    app = Flask(__name__)
    app.register_blueprint(site)
    return app

app = getApp()

to the run.py file for the time being.

1

So, here is what I found out from a different post. In my site=>routes, I declared a global blueprint with the name, site and later I created a site() function which somehow masked my site blueprint. If I just renamed the site method name, it works.

Natasha
  • 6,651
  • 3
  • 36
  • 58