使用iPython / Spyder从Python退出的问题 [英] problems exiting from Python using iPython/Spyder

查看:127
本文介绍了使用iPython / Spyder从Python退出的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前已经问过这个问题,但我已经尝试过相关问题的解决方案,例如这个无济于事。

This question has been asked before, but I have tried the solutions in related questions such as this to no avail.

我遇到了Python的退出命令的问题,我已经排除了由vanilla Python 3运行的代码问题。当我使用iPython或Spyder的iPython控制台运行时出现问题。

I am having problems with Python's exit command, and I have ruled out a problem with my code as run by vanilla Python 3. The problem comes when I run it with iPython or in Spyder's iPython console.

当我使用时一个简单的退出命令,我得到错误:

When I use just a simple exit command, I get the error:

NameError: name 'exit' is not defined

我已根据其他链接的建议导入了sys。唯一有用的是尝试sys.exit(),在这种情况下,我得到:

I have already imported sys as suggested by the other link. The only thing that kind of works is to try sys.exit() in which case I get:

An exception has occurred, use %tb to see the full traceback.

SystemExit

C:\Users\sdewey\AppData\Local\Continuum\Anaconda3\lib\site-
packages\IPython\core\interactiveshell.py:2870: UserWarning: To exit: use 
'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

我只说那种有用,因为错误信息较小,所以不那么讨厌:)。

I only say that that "kind of works" because the error message is smaller so it's less annoying :).

任何想法?似乎是iPython的一个问题。我在Jupyter(使用iPython)中遇到了一个不同的问题,其中完全忽略了quit,我单独发布了这个问题这里

Any ideas? Seems like an issue with iPython. I have encountered a different issue in Jupyter (which uses iPython) where quit was ignored entirely, which I posted about separately here

推荐答案

我在运行包含脚本的脚本时遇到了同样的问题在Pycharm的IPython shell中退出()。
我在这里学习了这个出口对于交互式shell,所以行为会有所不同,具体取决于shell如何实现它。

I've run into the same issue while running scripts containing exit() in Pycharm's IPython shell. I learned here, that exit is intended for interactive shells, so behaviour will vary depending on how the shell implements it.

我可以找到一个解决方案......

I could figure out a solution which would...


  • 退出时不杀内核

  • 不显示追溯

  • 不强迫你使用try / excepts巩固代码

  • 使用或不使用IPython,无需更改代码

  • not kill the kernel on exit
  • not display a traceback
  • not force you to entrench code with try/excepts
  • work with or without IPython, without changes in code

只需从下面的代码中导入'exit'到你打算用IPython运行的脚本,并调用'exit()'就可以了。您也可以在jupyter中使用它(而不是 quit ,这只是exit的另一个名称),它不会像在IPython shell中一样完全退出,让你知道...

Just import 'exit' from the code beneath into scripts you also intend to run with IPython and calling 'exit()' should work. You can use it in jupyter as well (instead of quit, which is just another name for exit), where it doesn't exit quite as silent as in the IPython shell, by letting you know that...

 An exception has occurred, use %tb to see the full traceback.

 IpyExit 







"""
# ipython_exit.py
Allows exit() to work if script is invoked with IPython without
raising NameError Exception. Keeps kernel alive.

Use: import variable 'exit' in target script with
     'from ipython_exit import exit'    
"""

import sys
from io import StringIO
from IPython import get_ipython


class IpyExit(SystemExit):
    """Exit Exception for IPython.

    Exception temporarily redirects stderr to buffer.
    """
    def __init__(self):
        # print("exiting")  # optionally print some message to stdout, too
        # ... or do other stuff before exit
        sys.stderr = StringIO()

    def __del__(self):
        sys.stderr.close()
        sys.stderr = sys.__stderr__  # restore from backup


def ipy_exit():
    raise IpyExit


if get_ipython():    # ...run with IPython
    exit = ipy_exit  # rebind to custom exit
else:
    exit = exit      # just make exit importable

这篇关于使用iPython / Spyder从Python退出的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆