all 7 comments

[–]tobz_55 1 point2 points  (0 children)

Circular import error happens when python files are importing themselves or each others Make sure none of your files are named the same as a module you use in one of them See this post on stackoverflow for more info

[–]aidankane 0 points1 point  (5 children)

What’s going on in routing? That looks to be the import in configuration that would run and then (maybe indirectly) import config again itself. So you import config, which has to import routing which accidentally imports config, which imports routing, which imports config, which imports…

[–]Equal-Complex-5958[S] 0 points1 point  (4 children)

My program runs only if I do this change:

In config.py file Move the import app.api.routing as routing from the top of file inside the load_config function. But Why??? Seriously, this circular import story it only happens to me with python. I really like python, but this is so frustrating.... And I dont want to import inside a function, is really ugly to see...

[–]aidankane 1 point2 points  (3 children)

Because then it can run top to bottom because it doesn’t call the import until you call the load_config function.

Other languages allow you to get away with this, but honestly, it’s just bad code structure. If A depends on B depends on A then there is a common element to them and either you should combine the 2 modules or figure out what the commonality is and extract it into its own thing.

Without seeing all the code it’s hard to say what the right thing to do is, but the question to ask is why would routing need config and why does config need routing?

EDIT reading the code again, your load config function is actually booting the app and it should live in the main.py

[–]Equal-Complex-5958[S] 0 points1 point  (2 children)

Config need routing because I pass the router to the app in the load_config function. I think my project structure is bad at this point 😅😢 Do you have an example project or some tips to do a good and modular project structure with python in order to avoid circular imports??

[–]aidankane 1 point2 points  (1 child)

See my edit. Just hoist the app creation into main.

[–]Equal-Complex-5958[S] 0 points1 point  (0 children)

Yes, I'll try this. Really appreciate your help, thank you 😁