How to import a module as __main__?

后端 未结 7 1130
借酒劲吻你
借酒劲吻你 2021-01-17 16:04

I have a module that has the usual

if __name__ == \'__main__\':
    do stuff...

idiom.

I\'d like to import that from another module

相关标签:
7条回答
  • 2021-01-17 16:43

    Put that code in a function, and call it from the module you are importing it into as well.

    def stuff():
        ...
    
    if __name__ == '__main__':
        stuff()
    

    And then in the module you are importing it into:

    import module
    module.stuff()
    
    0 讨论(0)
提交回复
热议问题