无法解决Python ImportError:无法导入名称“ FirstNews”

时间:2019-06-16 05:27:54

标签: python web inheritance web-crawler circular-dependency

如果我从A类,父类中选择菜单,然后移至B类,子类中,以及我从B类,子类中选择菜单,那么我目前正在使用Inheritance Class用python创建自己的项目。我想转到A类。如何解决此问题?

我尝试使用Python Decorator制作这些文件,但是Decorator可以使用一个文件。因此我尝试使用Python继承来释放A类和B类之间的移动,但无济于事。

这是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

    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

     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)

并且发生了错误。 追溯(最近一次通话):   文件“ E:/Python_Class/Web_Crawling_and_ChatBot_1/NaverNews/Main/Main.py”,第12行     从FirstNews导入FirstNews   文件“ E:\ Python_Class \ Web_Crawling_and_ChatBot_1 \ NaverNews \ Main \ News \ FirstNews.py”,第3行,在     从主要进口NaverNews   文件“ E:\ Python_Class \ Web_Crawling_and_ChatBot_1 \ NaverNews \ Main \ Main.py”,第12行     从FirstNews导入FirstNews ImportError:无法导入名称“ FirstNews”

1 个答案:

答案 0 :(得分:0)

您的FirstNews.py文件位于子文件夹News

Main文件中,尝试

from News.FirstNews import FirstNews

编辑:我意识到您的模块相互导入。这对我来说有点奇怪,因为调试器会混淆哪个是主文件。并且它将被卡在导入模块的循环中。您应该考虑更改代码结构。