【问题标题】:Python JupyterLab: Stop the execution of a code at a linePython JupyterLab:在一行停止执行代码
【发布时间】:2021-03-12 11:57:54
【问题描述】:

我有一段很长的代码,有时我不想执行所有代码,而只是停在某一行。要停止一行代码的执行,我执行以下操作:

print('Stop here: Print this line')
quit()
print('This line should not print because the code should have stopped')

正确的答案是只打印第一行。当我使用quit()quit exit()exit 时,两行都会打印。当我使用import sys 然后sys.exit() 时,我收到以下错误消息

发生了异常,使用 %tb 查看完整的回溯。 系统退出 C:\Users\user\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3351: 用户警告:要退出:使用“退出”、“退出”或 Ctrl-D。警告(“退出: 使用 'exit'、'quit' 或 Ctrl-D。", stacklevel=1)

我怎样才能执行在一行停止执行的任务?

【问题讨论】:

  • 我是否正确假设您这样做是为了调试?
  • 您是否尝试过引发异常?您可以添加类似raise ValueError 的内容,这将引发异常。
  • @MaMaNu:是的,主要用于调试,但有时我只想执行代码。
  • @Matt C:怎么做?当我添加raise ValueError 时出现错误---------------------------------------- ---------------------------------- ValueError Traceback(最近一次调用最后一次)中的cf8caaab243c>
  • sys.exit(0) 是正常退出程序的正确方式。退出代码0 告诉系统没有错误。如果 IPython 认为这是一个问题并发出警告,那么它就是一个 IPython“问题”

标签: python jupyter exit jupyter-lab terminate


【解决方案1】:

如果您尝试调试代码并且可能希望从停止的位置继续,您应该改用pdb

print('Stop here: Print this line')
import pdb; pdb.set_trace()
print('This line should not print because the code should have stopped')

当你执行它时,解释器会在set_trace() 行中断。然后将提示您 pdb 提示。您可以在此提示下检查变量的值。要继续执行,请按cq 以进一步退出执行。查看pdb的其他有用命令。

【讨论】:

    【解决方案2】:

    您似乎想在您选择的某个点停止代码

    为此,我在您的限制范围内找到了两种可能的方法。 一种是你简单地写

    raise Exception("Finished code")
    

    这将允许您停止代码并引发您自己的异常并编写您选择的任何异常。

    但是,如果您不希望有任何例外,那么我会将您指向此链接:https://stackoverflow.com/a/56953105/14727419

    【讨论】:

    • 当我运行这段代码时,我得到这个错误“------------------------------- -------------------------------------------- 异常回溯(最近一次调用last) in 1 print('Stop here: Print this line') ----> 2 raise Exception("Finished code") 3 print('这行不应该打印因为代码应该已经停止')异常:完成的代码
    • 是的,所以您提出了“已完成代码”异常。这不是一个错误,因为这就是 Python 中的 raise 命令所做的。在我发布的链接中,您应该会看到一种在不向您返回异常消息的情况下引发该异常的方法。
    【解决方案3】:

    这似乎是与 iPython 相关的问题,如 here 所见。

    如果您不想使用那里提供的解决方案,并且不介意强行终止该进程,您可以这样做:

    import os
    
    os.system('taskkill /F /PID %d' % os.getpid())
    

    【讨论】:

      【解决方案4】:

      出于调试目的,可以使用内置调试器pdb。以下链接提供了如何设置的教程:Debug Jupyter

      【讨论】:

        【解决方案5】:

        这是我必须防止意外执行管道底部的测试部分:

        import pdb
        
        # this line should capture the input, temporarily 
        # preventing subsequent notebook cells from being executed
        pdb.set_trace() 
        
        # this line causes pdb to exit with an error, which is required 
        # to stop subsequent cells from execution if user fails to type 
        # "exit" in pdb command line and just presses the Stop button in the Notebook interface
        raise Error("This line should fail to prevent cells below from being executed regardless of how pdb was exited!")
        

        【讨论】:

          猜你喜欢
          • 2018-09-29
          • 1970-01-01
          • 2021-11-04
          • 1970-01-01
          • 2012-02-27
          • 2012-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多