0

hello I'm trying to create my first fask REST API app in python.

I have created 3 packages.

Config package

config.py

host = 'localhost'
port = 8080
debug = True

controllers package

calendar.py

from server.server import app

@app.route('/')
@app.route('/home')
def home():
   return {"hello: word "}

server server.py

from flask import Flask
from controllers import calendar

app = Flask(__name__)

and one main.py

from server.server import app
from config import config

if __name__ == '__main__':
    app.run(host=config.host, port=config.port, debug=config.debug)

I'm getting the below error, where I'm going wrong?

ImportError: cannot import name 'app' from partially initialized module 'server.server' (most likely due to a circular import) (/Users/local/PycharmProjects/testflask/server/server.py)
  • It's exactly as the error message says: there is a circular import. `calendar` and `server.server` try to import `from` each other. This cannot work. Please see the linked duplicate for details on such problems. – Karl Knechtel Apr 14 '23 at 18:52
  • 1
    `server.py` doesn't even use `calendar`. Why is it imported? – John Gordon Apr 14 '23 at 18:52
  • sure @JohnGordon, but then how can I load the routes? – Manish Parab Apr 14 '23 at 18:56
  • @KarlKnechtel, I get the error, it's not duplicate. – Manish Parab Apr 14 '23 at 18:57
  • In the code you posted for `server.py`, it imports `calendar` but then does not use it. I don't know what you mean by "how can I load the routes?" `server.py` does not appear to do anything with routes... – John Gordon Apr 14 '23 at 18:59
  • @JohnGordon , please take a look at https://stackoverflow.com/questions/26688234/flask-app-does-not-use-routes-defined-in-another-module – Manish Parab Apr 14 '23 at 19:00
  • yeah, it does not use it but it loads the route when it's imported ?. sorry I'm new to python, so I'm confused, better question would be how can I wire up the routes in the controller with server? – Manish Parab Apr 14 '23 at 19:03

0 Answers0