简体   繁体   English

Python IDLE:运行main?

[英]Python IDLE: Run main?

I'm in IDLE: 我在IDLE:

>>> import mymodule
>>> # ???

After importing a module with: 使用以下命令导入模块后:

if __name__ == '__main__':
    doStuff()

How do I actually call main from IDLE? 我实际上如何从IDLE调用main

The if condition on __name__ == '__main__' is meant for code to be run when your module is executed directly and not run when it is imported. __name__ == '__main__'上的if条件旨在使代码在直接执行模块时运行,而在导入模块时不运行。 There is really no such concept of "main" as eg in Java. 实际上,没有像Java这样的“主”概念。 As Python is interpreted, all lines of code are read and executed when importing/running the module. 解释为Python后,在导入/运行模块时将读取并执行所有代码行。

Python provides the __name__ mechanism for you to distinguish the import case from the case when you run your module as a script ie python mymodule.py . Python提供了__name__机制,以区分导入大小写与作为脚本运行模块(即python mymodule.py时的大小写。 In this second case __name__ will have the value '__main__' 在第二种情况下, __name__的值为'__main__'

If you want a main() that you can run, simply write: 如果您希望可以运行main(),只需编写:

def main():
   do_stuff()
   more_stuff()

if __name__ == '__main__':
   main()

If you import something it's not main. 如果您导入的东西不是主要的。 You need to run it from the menu or as an argument when you start idle. 开始闲置时,需要从菜单运行它或将其作为参数运行。

I suppose that you are calling 'main' what you have after if __name__ == '__main__' . 我想if __name__ == '__main__' ',那么您称呼的是“ main if __name__ == '__main__' So to call it: 所以可以这样称呼:

>> import mymodule
>> mymodule.doStuff()

Otherwise if you actually have a main function in your module then, 否则,如果您的模块中实际上有一个主要功能,

>> import mymodule
>> mymodule.main()

I found another way to run a module by name in Python 2.6: 我找到了另一种在Python 2.6中按名称运行模块的方法:

>>> import runpy
>>> runpy.run_module('mypack.mymodule')

run_module returns a dictionary with all created attributes run_module返回具有所有已创建属性的字典

http://docs.python.org/library/runpy.html?highlight=runpy#runpy.run_module http://docs.python.org/library/runpy.html?highlight=runpy#runpy.run_module

Regarding the execfile mentioned by taleinat. 关于taleinat提到的execfile。

Python 3.5 deprecated execfile instead following method is suggested. 建议使用不建议使用Python 3.5的execfile,而建议使用以下方法。

Removed execfile(). 删除了execfile()。 Instead of execfile(fn) use exec(open(fn).read()). 代替execfile(fn)使用exec(open(fn).read())。

PS:I could not comment on taleinat's solution because of lack of reputation points in stackoverflow. PS:我无法评论taleinat的解决方案,因为stackoverflow中缺少信誉点。

All you would have to do is call the main function itself like joaquin said. 您需要做的就是像joaquin所说的那样调用main函数本身。 How I do it is just keep a terminal open at the location of the file and rerun the command when I need it. 我只是在文件的位置保持终端打开并在需要时重新运行命令。 A last method would be to use an IDE like geany or Idle and open it with (file>open) and push F5. 最后一种方法是使用geany或Idle之类的IDE,并通过(file> open)将其打开,然后按F5。
The: 该:

if __name__ == '__main__':
doStuff()

you have is actually made to keep the main function from running if it's imported. 实际上,如果导入了主函数,则可以防止主函数运行。

EDIT: Updated this old answer since execfile was removed in Python 3. 编辑:由于execfile在Python 3中已删除,因此更新了这个旧答案。

With Python 2 使用Python 2

Use execfile (ie execfile(file_path) ) instead of using import. 使用execfile (即execfile(file_path) )代替导入。

With Python 3 使用Python 3

Use the runpy module as suggested in Carles Barrobés's answer . 按照CarlesBarrobés的答案中的建议使用runpy模块。

Update: 更新:

The purpose of the condition if __name__ == '__main__' is to have the code inside the "if" block not be executed when importing the module. 条件if __name__ == '__main__'的目的是在导入模块时执行“ if”块内的代码。 Such code will only be run when the file is run directly, for example by running "python filename " from the command line, or by using execfile(_filename_) . 仅当直接运行文件时(例如,通过从命令行运行“ python filename ”或使用execfile(_filename_) ,才会运行此类代码。

As requested, an example of using execfile. 根据要求,提供了一个使用execfile的示例。 In C:\\my_code.py : C:\\my_code.py

if __name__ == '__main__':
    print "Hello World!"

Then, in an interpreter: 然后,在解释器中:

>>> execfile("C:\\my_code.py")
Hello world!

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

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