0

I have four files: menu.py,notebook.py, __init__.py and test.py which are aranged as @afaq suggested.

My 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)]

And menu.py: (edited)

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)

My __init__.py is empty

My test.py:

from Notebook.my_notebook.menu import Menu

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

When I run test.py, python returns me this error:

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

How can I fix this?

SiXUlm
  • 159
  • 3
  • 12
  • notebook is not global module, please read this http://stackoverflow.com/questions/1112618/import-python-package-from-local-directory-into-interpreter – cymruu Mar 12 '17 at 20:27
  • @Filip: thanks, I edit `.notebook` instead of `notebook` and got this new error. Can you help? – SiXUlm Mar 12 '17 at 20:31
  • Try `from . import notebook` – cymruu Mar 12 '17 at 20:35
  • python returns me this error `Traceback (most recent call last): File "menu.py", line 2, in from . import Note, Notebook ImportError: cannot import name 'Note'` – SiXUlm Mar 12 '17 at 20:37
  • @SiXUlm, I have updated the executor code. Please check if it works now. – Afaq Mar 12 '17 at 22:11
  • 1
    You don't have a `Notebook` package or anything named `my_notebook` or `menu`. Why were you expecting `from Notebook.my_notebook.menu import Menu` to do? How do you think import statements work? – user2357112 Mar 12 '17 at 22:21
  • Please don't refer to an existing question + answer without including the essentials here: make the question standalone. "arranged as @afaq suggested" doesn't mean anything (and you don't even provide a link to where that suggestion is, let alone what it is). –  Mar 12 '17 at 22:59

1 Answers1

1

Create if __name__ == "__main__": call in a separate module. You cannot do a relative import while trying to execute that module.

Check 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)

Your directory structure should look like this.

enter image description here

Your package structure should look like this.

enter image description here

And the executor code would look like this

enter image description here

Community
  • 1
  • 1
Afaq
  • 1,146
  • 1
  • 13
  • 25
  • Thanks! I will try and be back with the results. – SiXUlm Mar 12 '17 at 20:40
  • Thanks for your update. I'm not so clear at 3 points. First, in the first image, what is `notbook_menu`? Second, what is inside the `__init__.py`? Third, the executor code means, I will delete `if__name__ ...` line in the `menu.py`, am I right? – SiXUlm Mar 12 '17 at 20:54
  • `notebook_menu` is just random package name that i chose for your python files. `__init__` file is what makes your folder a python package. And yes when you are creating an executor like i did here , you won't need `if __name__ ...` anymore in menu.py. – Afaq Mar 12 '17 at 20:56
  • Sorry for the typo in `notebook_menu` in the images. – Afaq Mar 12 '17 at 20:59
  • No worries about the typo. I will keep trying and let you know what I get. Thanks again! – SiXUlm Mar 12 '17 at 21:04
  • Hi, I tried what you suggested but I still got this error (please see my update). Can you help? – SiXUlm Mar 12 '17 at 21:40
  • Do not change your existing `notebook.py` and `menu.py` code. Moreover I don't see `from Notebook.my_notebook.menu import Menu` in your code. Why is error pointing that ? – Afaq Mar 12 '17 at 21:45
  • Keep your `__init__.py` file empty. – Afaq Mar 12 '17 at 21:47
  • @SiXUlm . I have updated my answer with more help. Please check. – Afaq Mar 12 '17 at 21:59
  • But for me it is working fine and I can see the output as well. Not sure about your directory structure. – Afaq Mar 12 '17 at 22:03
  • Thanks! Finally it works now. I really appreciate your help! – SiXUlm Mar 13 '17 at 10:25