另一个“ImportError:无法导入名称”

时间:2017-03-12 20:25:21

标签: python python-import importerror

我有四个文件:menu.pynotebook.py__init__.pytest.py,这些文件是@afaq建议的。

我的notebook.py

class Note:
    def __init__(self,memo,tag = ''):
        self.memo = memo
        self.tag = tag

    def match(self,filter):
        '''determine if this note matches the filter text, return
        true if match, false if not'''
        return filter in self.memo or filter in self.tag

class Notebook:
    def __init__(self):
        '''ininitalize a notebook with empty list'''
        self.note = []

    def search(self,filter):
        '''Find all notes that match the given filter string.'''
        return [note for note in self.note if note.match(filter)]

menu.py :(已编辑)

import sys

from .notebook import Note, Notebook

class Menu:
    '''Display a menu and respond to choices when run.'''
    def __init__(self):
        self.notebook = Notebook()
        self.choices = {
            "1": self.search_note,
            "2": self.quit
        }

    def display_menu(self):
        print("""
            Notebook Menu
            1. Search Notes
            2. Quit
            """)

    def run(self):
        '''Display the menu and respond to choices.'''
        while True:
            self.display_menu()
            choice = input("Choice is: ")
            action = self.choices.get(choice)
            if action:
                action()
            else:
                print("{0} is not a valid choice".format(choice))

    def search_note(self):
        '''search for text and display all notes contain it'''
        filter = input("Search for: ")
        notes = self.notebook.search(filter)
        self.show_note()

    def quit(self):
        print("Bye bye")
        sys.exit(0)

我的__init__.py为空

我的test.py

from Notebook.my_notebook.menu import Menu

if __name__ == "__main__":
    Menu().run()

当我运行test.py时,python会返回此错误:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from Notebook.my_notebook.menu import Menu
ModuleNotFoundError: No module named 'Notebook'

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

在单独的模块中创建if __name__ == "__main__":电话。尝试执行该模块时,无法进行相对导入。

检查Ultimate answer to relative python imports

<强> menu.py

import sys

from .notebook import Note, Notebook

class Menu:
    '''Display a menu and respond to choices when run.'''
    def __init__(self):
        self.notebook = Notebook()
        self.choices = {
            "1": self.search_note,
            "2": self.quit
        }

    def display_menu(self):
        print("""
            Notebook Menu
            1. Search Notes
            2. Quit
            """)

    def run(self):
        '''Display the menu and respond to choices.'''
        while True:
            self.display_menu()
            choice = input("Choice is: ")
            action = self.choices.get(choice)
            if action:
                action()
            else:
                print("{0} is not a valid choice".format(choice))

    def search_note(self):
        '''search for text and display all notes contain it'''
        filter = input("Search for: ")
        notes = self.notebook.search(filter)
        self.show_note()

    def quit(self):
        print("Bye bye")
        sys.exit(0)

您的目录结构应如下所示。

enter image description here

您的包结构应如下所示。

enter image description here

执行者代码看起来像这样

enter image description here