当导入到另一个模块时,Python正在从该模块打印所有内容

当导入到另一个模块时,Python正在从该模块打印所有内容,python,python-module,python-import,Python,Python Module,Python Import,我有两个文件 funcattrib.py test\u import.py funcattrib.py import sys def sum(a,b=5): "Adds two numbers" a = int(a) b = int(b) return a+b sum.version = "1.0" sum.author = "Prasad" k = sum(1,2) print(k) print("Function attributes: - ") pri

我有两个文件

  • funcattrib.py
  • test\u import.py
  • funcattrib.py

    import sys
    
    def sum(a,b=5):
        "Adds two numbers"
        a = int(a)
        b = int(b)
        return a+b
    
    sum.version = "1.0"
    sum.author = "Prasad"
    k = sum(1,2)
    print(k)
    
    print("Function attributes: - ")
    print("Documentation string:",sum.__doc__)
    print("Function name:",sum.__name__)
    print("Default values:",sum.__defaults__)
    print("Code object for the function is:",sum.__code__)
    print("Dictionary of the function is:",sum.__dict__)
    
    #writing the same information to a file
    
    f = open('test.txt','w')
    f.write(sum.__doc__)
    f.close()
    print("\n\nthe file is successfully written with the documentation string")
    
    import sys
    from funcattrib import sum
    
    input("press <enter> to continue")
    
    a = input("Enter a:")
    b = input("Enter b:")
    f = open('test.txt','a')
    matter_tuple = "Entered numbers are",a,b
    print(matter_tuple)
    print("Type of matter:",type(matter_tuple))
    matter_list = list(matter_tuple)
    print(list(matter_list))
    finalmatter = " ".join(matter_list)
    print(finalmatter)
    f.write(finalmatter)
    f.close()
    print("\n\nwriting done successfully from test_import.py")
    
    测试导入.py

    import sys
    
    def sum(a,b=5):
        "Adds two numbers"
        a = int(a)
        b = int(b)
        return a+b
    
    sum.version = "1.0"
    sum.author = "Prasad"
    k = sum(1,2)
    print(k)
    
    print("Function attributes: - ")
    print("Documentation string:",sum.__doc__)
    print("Function name:",sum.__name__)
    print("Default values:",sum.__defaults__)
    print("Code object for the function is:",sum.__code__)
    print("Dictionary of the function is:",sum.__dict__)
    
    #writing the same information to a file
    
    f = open('test.txt','w')
    f.write(sum.__doc__)
    f.close()
    print("\n\nthe file is successfully written with the documentation string")
    
    import sys
    from funcattrib import sum
    
    input("press <enter> to continue")
    
    a = input("Enter a:")
    b = input("Enter b:")
    f = open('test.txt','a')
    matter_tuple = "Entered numbers are",a,b
    print(matter_tuple)
    print("Type of matter:",type(matter_tuple))
    matter_list = list(matter_tuple)
    print(list(matter_list))
    finalmatter = " ".join(matter_list)
    print(finalmatter)
    f.write(finalmatter)
    f.close()
    print("\n\nwriting done successfully from test_import.py")
    
    导入系统 从funcattrib导入和 输入(“按继续”) a=输入(“输入a:”) b=输入(“输入b:”) f=打开('test.txt','a') matter\u tuple=“输入的数字是”,a、b 打印(物质组) 打印(“物质类型:,类型(物质组)) 物质列表=列表(物质元组) 打印(列表(事项列表)) finalmatter=“”.加入(事项列表) 打印(最终材料) f、 写入(最终材料) f、 关闭() 打印(“\n\n从test\u import.py成功完成写入”) 我从
    funcattrib.py
    导入了
    sum
    函数。当我尝试执行test_import.py时,我看到了整个
    funcattrib.py
    的输出。我只想使用
    sum
    函数


    请告知,我做错了什么,或者有没有其他方法可以在不实际执行模块的情况下导入模块?

    导入模块“顶层”中的所有语句时都会执行

    如果不希望发生这种情况,则需要区分作为脚本使用的模块和模块。使用以下测试进行测试:

    if __name__ == '__main__':
        # put code here to be run when this file is executed as a script
    
    将其应用于您的模块:

    import sys
    
    def sum(a,b=5):
        "Adds two numbers"
        a = int(a)
        b = int(b)
        return a+b
    
    sum.version = "1.0"
    sum.author = "Prasad"
    
    if __name__ == '__main__':
        k = sum(1,2)
        print(k)
    
        print("Function attributes: - ")
        print("Documentation string:",sum.__doc__)
        print("Function name:",sum.__name__)
        print("Default values:",sum.__defaults__)
        print("Code object for the function is:",sum.__code__)
        print("Dictionary of the function is:",sum.__dict__)
    
        #writing the same information to a file
    
        f = open('test.txt','w')
        f.write(sum.__doc__)
        f.close()
        print("\n\nthe file is successfully written with the documentation string")
    

    导入时,将执行模块“顶层”中的所有语句

    如果不希望发生这种情况,则需要区分作为脚本使用的模块和模块。使用以下测试进行测试:

    if __name__ == '__main__':
        # put code here to be run when this file is executed as a script
    
    将其应用于您的模块:

    import sys
    
    def sum(a,b=5):
        "Adds two numbers"
        a = int(a)
        b = int(b)
        return a+b
    
    sum.version = "1.0"
    sum.author = "Prasad"
    
    if __name__ == '__main__':
        k = sum(1,2)
        print(k)
    
        print("Function attributes: - ")
        print("Documentation string:",sum.__doc__)
        print("Function name:",sum.__name__)
        print("Default values:",sum.__defaults__)
        print("Code object for the function is:",sum.__code__)
        print("Dictionary of the function is:",sum.__dict__)
    
        #writing the same information to a file
    
        f = open('test.txt','w')
        f.write(sum.__doc__)
        f.close()
        print("\n\nthe file is successfully written with the documentation string")
    

    Python总是从上到下执行。函数定义和其他任何东西一样都是可执行代码。导入模块时,将运行该模块顶层的所有代码。Python必须全部运行,因为函数是代码的一部分

    解决方案是保护您不希望在导入时在
    下运行的代码,如果uuuu name\uuuuu==“\uuuuuuu main\uuuu”
    块:

    if __name__ == "__main__":
         print("Some info")
    

    Python总是从上到下执行。函数定义和其他任何东西一样都是可执行代码。导入模块时,将运行该模块顶层的所有代码。Python必须全部运行,因为函数是代码的一部分

    解决方案是保护您不希望在导入时在
    下运行的代码,如果uuuu name\uuuuu==“\uuuuuuu main\uuuu”
    块:

    if __name__ == "__main__":
         print("Some info")