1
def add():
    import add_coffee_record
    import imp
    imp.reload(add_coffee_record)

def show():
    import show_coffee_records
    import imp
    imp.reload(show_coffee_records)

def search():
    import search_coffee_records
    import imp
    imp.reload(search_coffee_records)

def modify():
    import modify_coffee_records
    import imp
    imp.reload(modify_coffee_records)

def delete():
    import delete_coffee_record
    import imp
    imp.reload(delete_coffee_record)

def main():
    num=input('\nEnter the number on the menu: ')
    while num != '6':
        if num == '1':
            print()
            add()
        if num == '2':
            print()
            show()
        if num == '3':
            print()
            search()
        if num == '4':
            print()
            modify()
        if num == '5':
            print()
            delete()
        num=input('\nEnter the number on the menu: ')

main()

My output looks like so:

Enter the number on the menu: 2

Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0
Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0

Enter the number on the menu: 2

Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0

Enter the number on the menu: 2

Description: Thanksgiving Blend
Quantity: 300.0
Description: Christmas Blend
Quantity: 100.0

I'd like it if the first output would not duplicate itself. Is there a way to structure the program where on the first instance "import" ONLY is used and on successive instances "reload" is used? Note: I cannot copy paste the program files instead of import. I need to use import. Thanks.

3
  • Perhaps try: imp.reload(module); except ImportError: import module? Commented Nov 4, 2016 at 6:15
  • 1
    I think you're confused with the use of import. Its purpose is not to run some code, it is to make a module available in current context. Although it does run the module to do that the first time, you should not use that for anything more than initial setup. — Here, you should do all imports on top, and have them define functions that you call after. Commented Nov 4, 2016 at 6:16
  • NB: imp has been superseded by importlib in Python 3, and imp is essentially deprecated. Commented Nov 4, 2016 at 6:16

2 Answers 2

1

You could remove the module from sys.modules before importing it:

def add():
    sys.modules.pop("add_coffee_record")
    import add_coffee_record

Another option would be to call imp.load_module directly:

def add():
    imp.load_module("add_coffee_record", *imp.find_module("add_coffee_record"))
Sign up to request clarification or add additional context in comments.

2 Comments

Bingo Francisco. Thank YOU.
@ChanMan> You still should not do this. Those are internal details, and it uses them through a deprecated API. Learn to use imports correctly instead.
0

You need to go into the programs you're importing and change the function name from main to something else like add. Then comment out calling the function so you can call it in your menu program, and import the function name.

from add_coffee_record import add
from delete_coffee_record import delete
from modify_coffee_records import mod
from search_coffee_records import search
from show_coffee_records import show


def main():
    choice = -1
    print("Welcome!")
    while choice != 9:
        print ('Select 1 to add records,')
        print('Select 2 to show coffee records')
        print('Select 3 to search coffee records,')
        print('Select 4 to modify coffee records')
        print('Select 5 to delete coffee records.')
        print ('    or 9 to quit.')
        choice = int(input('Enter your choice: '))
        if choice == 1:
            add()
        elif choice==2:
            show()

        elif choice==3:
            search()
    elif choice==4:
        mod()
    elif choice==5:
        delete()
    elif choice==9:
        print('Thank you.')

main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.