if __name__ == '__main__' in IPython

前端 未结 4 696
情话喂你
情话喂你 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:04

    When working from within Emacs (which I assume is close to what you get with %edit), I usually use this trick:

    if __name__ == '__main__' and '__file__' in globals():
        # do what you need
    

    For obvious reasons, __file__ is defined only for import'ed modules, and not for interactive shell.

提交回复
热议问题