简体   繁体   English

无法解决 Python ImportError: cannot import name 'FirstNews'

[英]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?我目前正在使用继承类使用 Python 制作我自己的项目,如果我从 A 类,父类中选择菜单,然后移动到 B 类,子类,如果我从 B 类,子类中选择菜单,我会喜欢转A班,这个问题怎么解决?

I tried to make these files with Python Decorator, but Decorator can use one file.我尝试用 Python Decorator 制作这些文件,但 Decorator 只能使用一个文件。 so I tried to use Python Inheritance to free movement between Class A and Class B, but to no avail.所以我尝试使用 Python 继承在 A 类和 B 类之间自由移动,但无济于事。

Main.py:主要.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:第一新闻.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:默认.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'

Your FirstNews.py file is located in sub folder News您的FirstNews.py文件位于子文件夹News

In Main file, tryMain文件中,尝试

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.您应该考虑更改代码结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM