How to import a module as __main__?

后端 未结 7 1314
借酒劲吻你
借酒劲吻你 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:26

    As pointed out in the other answers, this is a bad idea, and you should solve the issue some other way.

    Regardless, the way Python does it is like this:

    import runpy
    result = runpy._run_module_as_main("your.module.name"))
    

提交回复
热议问题