Python 交互式打印引发运行时错误:事件循环已在运行

Python 交互式打印引发运行时错误:事件循环已在运行,python,python-2.7,jupyter-notebook,python-asyncio,interactive,Python,Python 2.7,Jupyter Notebook,Python Asyncio,Interactive,我正在使用Matplotlib运行一个交互式绘图,以拾取用于分析的某些数据点。这个程序在我的OSX计算机上运行得很好,它运行的是一个非常古老的版本。。。一切。然而,它在我的Windows机器上不能正常工作,我也不知道为什么。我可以让它正常工作,但每次我按一个键告诉我的代码要做什么,它都会抛出一个运行时异常: Traceback (most recent call last): File "C:\Users\mattk\Anaconda2\lib\site-packages\matp

我正在使用Matplotlib运行一个交互式绘图,以拾取用于分析的某些数据点。这个程序在我的OSX计算机上运行得很好,它运行的是一个非常古老的版本。。。一切。然而,它在我的Windows机器上不能正常工作,我也不知道为什么。我可以让它正常工作,但每次我按一个键告诉我的代码要做什么,它都会抛出一个运行时异常:

Traceback (most recent call last):
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\cbook\__init__.py", line 387, in process
    proxy(*args, **kwargs)
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\cbook\__init__.py", line 227, in __call__
    return mtd(*args, **kwargs)
  File "<ipython-input-3-aec763f461ae>", line 209, in press
    plt.waitforbuttonpress()
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\pyplot.py", line 724, in waitforbuttonpress
    return gcf().waitforbuttonpress(*args, **kwargs)
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\figure.py", line 2201, in waitforbuttonpress
    return blocking_input(timeout=timeout)
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\blocking_input.py", line 373, in __call__
    BlockingInput.__call__(self, n=1, timeout=timeout)
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\blocking_input.py", line 115, in __call__
    self.fig.canvas.start_event_loop(timeout=timeout)
  File "C:\Users\mattk\Anaconda2\lib\site-packages\matplotlib\backends\backend_qt5.py", line 477, in start_event_loop
    raise RuntimeError("Event loop already running")
RuntimeError: Event loop already running
这个例子,毫无疑问,在我按下'm'的前三次时运行良好,但从那时起,它抛出了我上面发布的错误。我不明白是什么导致了这个错误,计算机之间有什么不同,或者最重要的是如何消除这个错误。代码本身并不十分稳定,并且在运行几次之后会使Python崩溃,但这在两台机器上都很常见,可能与此问题无关。尽管有此错误消息,该程序仍能正常运行,但在每一行输出之间获取它会使其在实践中很难使用

我的机器是Windows 10,我运行的是Python 2.7,所有软件包的安装都是合理的最新版本

编辑2020年11月17日

更多信息:这可能是Jupyter笔记本和asyncio的一个众所周知的问题,因为安装nest_asyncio是被禁止的解决方案。问题是nest_asyncio需要Python3.5或更高版本,但我仍然坚持使用Python2.7。还有其他可能的解决办法吗

import matplotlib.pyplot as plt
%matplotlib qt

plt.rcParams["keymap.quit"] = ""   # Frees up q

# Define a function to understand key inputs in the figure to be drawn.
def press(event):

    if event.key == "m":

        mevent = fig.canvas.mpl_connect("pick_event", m_event)
        plt.waitforbuttonpress()
        fig.canvas.mpl_disconnect(mevent)

    # Define the key for exiting the while loop.
    elif event.key == "q":

        # Set the finished boolian to global so this function can change it.
        global finished

        # Set the finished boolian to true, allowing the loop to terminate.
        finished = True

        # Print a message informing the user to click on the plot to end the loop.
        print "Click plot to finish."
        plt.waitforbuttonpress()

# Define what happens when m is pressed.
def m_event(event):

    # Select the point.
    index = event.ind

    # Redraw the figure.
    fig.canvas.draw()

# Set up the interactive plot.
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
plot = ax.scatter([0, 1], [0, 1], picker=5)
fig.show()

# Link the button press event defined above.
fig.canvas.mpl_connect("key_press_event", press)

# Create a variable to determine when the loop ends.
finished = False

# Run a while loop where the user will interact with the plot.
while finished != True:

    # Stop running code and allow the user to interact with the plot indefinitely.
    plt.ginput()