if __name__ == '__main__' in IPython

前端 未结 4 693
情话喂你
情话喂你 2021-01-01 13:30

I have Python scripts that use the if __name__ == \'__main__\' trick to have some code only run when the script is called as a script and not when it is loaded

4条回答
  •  [愿得一人]
    2021-01-01 14:09

    It sounds like you might just need the -x switch:

    In [1]: %edit
    IPython will make a temporary file named: /tmp/ipython_edit_J8j9Wl.py
    Editing... done. Executing edited code...
    Name is main -- executing
    Out[1]: "if __name__ == '__main__':\n    print 'Name is main -- executing'\n"
    
    In [2]: %edit -x /tmp/ipython_edit_J8j9Wl
    Editing...
    

    When you call %edit -x the code is not executed after you exit your editor.

提交回复
热议问题