从另一个模块导入一个模块

时间:2019-10-19 08:09:25

标签: python python-3.x

我正在从另一个模块导入一个模块,所以我使用以下语法

if __name__ == '__main__':
    main()
    try:
        print("sorry We can't Let use this module To another Framework :")
    except Exception:
        print("yup")
    sys.exit(0)

如何停止阻止从另一个模块导入一个模块,以便每当我调用上一个模块时,它将运行sys.exit(status)并且模块导入过程将失败?

2 个答案:

答案 0 :(得分:3)

如果我理解您没错,解决方法如下:

if __name__ == '__main__':
    print("didn't get imported")
    # do some magic
else:
    print("got imported")
    exit(1)

答案 1 :(得分:-1)

#think i found better solution myself 
#so what if i write operation stuffs after calling main() #instead of writing those in main method like an example

def main() :
    return 

if  __name__=='__main__' :
     main() #calling main
   #todo operation stuffs#####

#Note: So if this module call by another module then it won't provide any operation stuffs just because main function has nothing to return. But in this module after calling main rest of the operations will start executing