How to import a module as __main__?

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

    The correct answer has been already given however it is confined in a comments (see How to import a module as __main__? and How to import a module as __main__?).

    The same with proper formatting:

    import runpy
    runpy.run_module("your.module.name", {}, "__main__")
    

    or

    import runpy
    runpy.run_path("path/to/my/file.py", {}, "__main__")
    

提交回复
热议问题