0

In my Django App I have a module dropdown

dropdown
  __init_.py
  select.py
  inline.py

In select.py have a class

from inline import doauto
class AbstractOption(ABC):
  def method1:
  def method2:
class Select(AbstractOption):
  def load_request:
  def method_helper:
    ....
     doauto()
    ....

In inline.py i have a class

class Inlineselect(AbstractOption):
   def load_request:
   def method_helper:

def doauto:
 inl= Inlineselect()
 inl.load_request()

I am getting following error: ImportError: cannot import name AbstractOption from select (/../../select.py)

I am not getting why this issue is coming. in inline.py i imported AbstractOption class from select.py and when I am using a child class from inline in select module class it gives class import error.

akirti
  • 179
  • 2
  • 15
  • 1
    You are doing `from select.py import AbstractOption`, right? I guess it's an issue of circular imports. Check [this](https://stackoverflow.com/questions/22187279/python-circular-importing) question and others related. – sanyassh Feb 24 '19 at 14:17
  • 2
    Possible duplicate of [Circular (or cyclic) imports in Python](https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python) – MisterMiyagi Feb 24 '19 at 14:28
  • select is a built-in module. You should probably name your module something else... (or learn about relative vs. absolute imports). – thebjorn Feb 24 '19 at 15:34
  • @thebjorn my filename is different. – akirti Feb 24 '19 at 16:17
  • You need to provide sample code that displays your problem. – thebjorn Feb 24 '19 at 16:21

1 Answers1

0

It is called circular import problem. Whenever you import something from other modules or packages, that something needs to be defined, before that can be imported. Below code should solve the problem.

select.py

from abc import ABC

class AbstractOption(ABC):
    def method1:
        pass

    def method2:
        pass

from inline import doauto

class Select(AbstractOption):
    def load_request:
        pass

    def method_helper:
        doauto()

inline.py

from select import AbstractOption

class Inlineselect(AbstractOption):
    def load_request:
        pass

    def method_helper:
        pass

def doauto:
    inl = Inlineselect()
    inl.load_request()
RyuCoder
  • 1,714
  • 2
  • 13
  • 21