【问题标题】:How to import and then reload without initial duplicate如何在没有初始重复的情况下导入然后重新加载
【发布时间】:2023-03-19 15:25:01
【问题描述】:
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()

我的输出如下所示:

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

如果第一个输出不会复制自己,我会喜欢它。有没有办法构建程序,在第一个实例上只使用“导入”,而在后续实例上使用“重新加载”?注意:我不能复制粘贴程序文件而不是导入。我需要使用导入。谢谢。

【问题讨论】:

  • 或许try: imp.reload(module); except ImportError: import module?
  • 我认为您对import 的使用感到困惑。它的目的不是运行某些代码,而是使模块在当前上下文中可用。尽管它确实第一次运行模块来执行此操作,但您不应将其用于初始设置之外的任何其他操作。 — 在这里,您应该在顶部执行所有导入,并让它们定义您之后调用的函数。
  • 注意:imp 在 Python 3 中已被 importlib 取代,imp 基本上已被弃用。

标签: python function python-3.x import while-loop


【解决方案1】:

您可以在导入之前从sys.modules 中删除该模块:

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

另一种选择是直接致电imp.load_module

def add():
    imp.load_module("add_coffee_record", *imp.find_module("add_coffee_record"))

【讨论】:

  • 宾果弗朗西斯科。谢谢。
  • @ChanMan> 你仍然不应该这样做。这些是内部细节,它通过已弃用的 API 使用它们。学习正确使用导入。
【解决方案2】:

您需要进入您正在导入的程序并将函数名称从 main 更改为其他名称,例如 add。然后注释掉调用该函数,以便您可以在菜单程序中调用它,并导入函数名称。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多