Search code examples
pythonwebinheritanceweb-crawlercircular-dependency

Cannot solve Python ImportError: cannot import name 'FirstNews'


I'm currently making my own project with Python using Inheritance Class between if I select menu from Class A, Parents Class, then moves to Class B, Child Class, and also if I select menu from Class B, Child Class, I'd like to move to Class A. How to solve this problem?

I tried to make these files with Python Decorator, but Decorator can use one file. so I tried to use Python Inheritance to free movement between Class A and Class B, but to no avail.

Main.py:

    # this is the Main.py
    from FirstNews import FirstNews
    from Default import Default

    import datetime
    import random

    random.seed(datetime.datetime.now())

    class NaverNews:
        def __init__(self, news_lists):
        self.FirstNewsSite = news_lists
        self.Default = news_lists

        def __call__(self):
            print("Select the News site that you want to read")
            menu_input = input("Selection Menu")
            menu_list = []
            while 1:
                if menu_input is 1:
                    self.FirstNewsSite()
                    menu_list.append(FirstNews)

                else:
                    self.Default()
                    menu_list.append(Default)
                    break

FirstNews.py:

    # This is the FirstNews.py
    import sys

    from Main import NaverNews
    from Default import Default

    sys.path.append('/NaverNews/Main/Main')


    # noinspection PyCallByClass
    class FirstNews(NaverNews):
    def __init__(self, my_choice):
        NaverNews.__init__(self, my_choice)
        self.myFirstChoice = my_choice
        self.mySecondChoce = my_choice
        self.myBackMenu = my_choice
        self.myDefault = my_choice

    def __call__(self):
        print("Select the News Company that you want to read")
        FirstInput = input()
        FirstList = []

        while 1:
            if FirstInput is 1:
                self.myFirstChoice()
                FirstList.append(NaverNews)
                # print("실행")

            elif FirstInput is 2:
                print("Going Back to Main.py")
                self.myBackMenu()
                FirstList.append(NaverNews)

            else:
                self.Default()
                FirstList.append(Default)
                break

Default.py:

    # This is the Default.py
    import sys

    from Main import NaverNews

    sys.path.append('/NaverNews/Main/Main')


    class Default(NaverNews):
        def __init__(self, myDefaultMenu):
            NaverNews.__init__(self, myDefaultMenu)
            self.myDefaultMenu = myDefaultMenu

        def __call__(self):
            print("You chose the wrong button\n Would you like to choose again? [Y / N]")
            DefaultInput = input()
            DefaultList = []

            if DefaultInput is 'y' or 'Y':
                self.myDefaultMenu()
                DefaultList.append(NaverNews)

            else:
                print("Shut down the program")
                exit(0)

And there are Errors that occurred.

Traceback (most recent call last):
  File "E:/Python_Class/Web_Crawling_and_ChatBot_1/NaverNews/Main/Main.py", line 12, in <module>
    from FirstNews import FirstNews
  File "E:\Python_Class\Web_Crawling_and_ChatBot_1\NaverNews\Main\News\FirstNews.py", line 3, in <module>
    from Main import NaverNews
  File "E:\Python_Class\Web_Crawling_and_ChatBot_1\NaverNews\Main\Main.py", line 12, in <module>

    from FirstNews import FirstNews
ImportError: cannot import name 'FirstNews'

Solution

  • Your FirstNews.py file is located in sub folder News

    In Main file, try

    from News.FirstNews import FirstNews
    

    EDIT: i realize that your modules import each other. This is a little bit weird to me, since the debugger would be confused about which one is the main file. And it will be stuck in a loop of importing module. You should consider changing your code structure.