0

Probably gonna be flagged as repeat question but all of the other posts don't seem to fix my issue. I have two classes. They were both in the same file before and now that I am separating them I am getting a circular import error.

One class:

import pad
class Dock(object):
      def someFunction(self):
          pad.Pad(...)

The other:

import dock
class Pad(dock.Dock):
      ...

That is the extent of it. Those are the only calls. The names of the files are lowercase of versions of the class (class Pads is in file pads.py and class Dock in file dock.py). I say this because some people mentioned file names causing issues. I'm confused on how to get them both imported when they both call each other.

Thanks for any help, I know it is probably a dumb question but save my monitor please.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Harry Manback
  • 41
  • 1
  • 6
  • If Dock imports Pad, and Pad imports Dock, you're creating an infinite loop of importing eachother. That's what a "circular import" is. [Does this answer your question?](https://stackoverflow.com/questions/5748946/pythonic-way-to-resolve-circular-import-statements) – Libra Jul 20 '22 at 20:03
  • this is indeed a circular import and will not work. if you have `import dock` in your `pad.py` module and `import pad` in your `dock.py` module you have a circular import. – hiro protagonist Jul 20 '22 at 20:04
  • So how do I allow them to access each other? I mean I guess I could toss the pad class back into the same file as the dock class just to have it work but I would like to have it a bit cleaner. – Harry Manback Jul 20 '22 at 20:44
  • It may be a design issue that `Dock` class create instance of `Pad` which is inherited from `Dock` itself. – acw1668 Jul 21 '22 at 02:22
  • 1
    You can move the line `import pad` to inside `someFunction()`. – acw1668 Jul 21 '22 at 02:31
  • Thanks @acw1668, that seemed to work. Never would have crossed my mind tbh. Always in a habit of having imports at the top of a file. Just seems odd although I'm sure it is normal lol – Harry Manback Jul 22 '22 at 16:20

0 Answers0