简体   繁体   English

如何将模块导入为 __main__?

[英]How to import a module as __main__?

I have a module that has the usual我有一个具有通常的模块

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

idiom.成语。

I'd like to import that from another module, and fool it into running that code.我想从另一个模块导入它,并欺骗它运行该代码。 Is there any way of doing this?有没有办法做到这一点?

I should mention, for reasons I won't go into here, I'm not in a position to change the code in the imported module.我应该提到,由于我不会在这里讨论的原因,我无法更改导入模块中的代码。 I need to somehow modify the import procedure so that it's name is main when imported, perhaps using ihooks or similar.我需要以某种方式修改导入过程,以便在导入时它的名称是主要的,也许使用 ihooks 或类似的。

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:无论如何,Python 的方式是这样的:

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

There is, execute the script instead of importing it.有,执行脚本而不是导入它。 But I consider this an extremely hackish solution.但我认为这是一个非常黑客的解决方案。

However the ideal pattern would be:然而,理想的模式是:

def do_stuff():
    ... stuff happens ...

if __name__ == '__main__':
    do_stuff()

that way you can do:这样你可以这样做:

from mymodule import do_stuff
do_stuff()

EDIT : Answer after clarification on not being able to edit the module code.编辑:在澄清无法编辑模块代码后回答。

I would never recommend this in any production code, this is a "use at own risk" solution.我永远不会在任何生产代码中推荐这个,这是一个“自担风险”的解决方案。

import mymodule

with open(os.path.splitext(mymodule.__file__)[0] + ".py") as fh:
    exec fh.read()

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()

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__? ).已经给出了正确答案,但它仅限于评论(请参阅如何将模块导入为 __main__?如何将模块导入为 __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__")

Code in a main stanza usually never makes sense to run directly.主节中的代码直接运行通常没有意义。 If you want to run it then use subprocess to run it in another Python interpreter.如果要运行它,请使用subprocess在另一个 Python 解释器中运行它。

Here is an example of a main module in Python:以下是 Python 中主模块的示例:

#! /usr/bin/env python
import sys
import os

def main(arg1, arg2, arg3):
    print(arg1, arg2, arg3)

if __name__ == "__main__":
    main(*sys.argv)

But you can also include但你也可以包括

def main():
   #The module contains Python code specific to the library module, 
   #like tests, and follow the module with this:

if __name__ == "__main__":
    main(*sys.argv)

in any module you would like to run as main.在任何你想作为主运行的模块中。

For example, if you have a library module, you can always use this construct to execute something specific like tests.例如,如果你有一个库模块,你总是可以使用这个结构来执行一些特定的事情,比如测试。

Put it in a function:把它放在一个函数中:

def _main():
   do stuff

if __name__ == '__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM