ImportError: cannot import name 'Room' from partially initialized module

I started learning Django some weeks back and i am encountering a particular error.

Import Error: cannot import Room from partially initialized module.

Pls how can I fix this, have been to stackflow and other sites I can not find the solutions

1 Like

It looks like you have a circular import. The module ola.views probably imports ola.models either directly or through a chain of imports which makes an import loop. This is a problem because normally you cannot import the same module again before the first import finished (the execution reached the end of the module file).

Probably the best practice is to split the modules in a way that the required imports make a tree dependency structure (without loops).

1 Like

Hello @vbrozik,

I am facing the same error, how can I do split the modules in a way that the required imports make a tree dependency structure (without loops) ?
can you elaborate please !
Thank you
Best regards
Oussama

The basic way:

  • Analyze the objects and their dependencies.
  • If there are too many two-way dependencies (spaghetti code) refactor the code to limit them.
  • The dependencies will suggest you a way where to split.

Normally an experienced programmer keeps this in mind during designing the code structure to limit the need for future refactorings. It is a good idea to draw (or imagine) a diagram of objects and their dependencies before we start programming them.