【问题标题】:Spyder IDE: Need to press 'run script' twice to successfully execute Python scriptSpyder IDE:需要按两次“运行脚本”才能成功执行 Python 脚本
【发布时间】:2016-10-22 02:51:19
【问题描述】:

我目前正在使用 Python(Spyder IDE、Python 3.5、Anaconda 4.0.0)编写图像处理脚本。当我第一次打开 IDE 时,我只需按一次“运行脚本”即可执行脚本。但在那之后,我必须按两次“运行脚本”,有时甚至按三下,才能执行。在互联网上搜索,似乎问题与使用matplotlibpyplot 有关。这主要是一个问题,因为我将在每次测试中花费 5 分钟来执行我的脚本。我的代码包含在下面。我决定在这里询问这个问题,看看是否有人有建议或想法让我的脚本在第一次印刷时执行。

编辑:每当我重新启动内核(启动新控制台)时,我都能在第一次按下时运行脚本。

import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data, img_as_float 
from skimage.filters import gaussian 
from skimage.segmentation import active_contour 
from skimage import io 

from skimage import exposure 

import scipy 
scipy_version = list(map(int, scipy.__version__.split('.')))
new_scipy = scipy_version[0] > 0 or \
            (scipy_version[0] == 0 and scipy_version[1] >= 14)

'''
img = data.astronaut()
img = rgb2gray(img)
'''


openLocation = "file location here"

img = io.imread(openLocation)
#img = rgb2gray(img)



s = np.linspace(0, 2*np.pi, 600)
x = 400 + 300*np.cos(s)
y = 550 + 280*np.sin(s)
init = np.array([x, y]).T

if not new_scipy:
    print('You are using an old version of scipy. '
          'Active contours is implemented for scipy versions '
          '0.14.0 and above.')



if new_scipy:
    snake = active_contour(img, init, alpha=0.01, beta=0.01, w_line = 5, w_edge = 0, gamma=0.01, bc = 'periodic')

    fig = plt.figure(figsize=(7, 7))
    ax = fig.add_subplot(111)
    plt.gray()
    ax.imshow(img)
    ax.plot(init[:, 0], init[:, 1], '--r', lw=3)
    ax.plot(snake[:, 0], snake[:, 1], '-b', lw=3)
    ax.set_xticks([]), ax.set_yticks([])
    ax.axis([0, img.shape[1], img.shape[0], 0])

【问题讨论】:

    标签: python matplotlib spyder


    【解决方案1】:

    我认为您的问题与this 有关,它是spyder 中的一个错误。到目前为止,它提供的部分解决方案是使用不同的 Matplotlib 后端。你可以改变它:

    Preferences > Console > External modules > Matplotlib
    

    从默认 (Qt4Agg) 到 TkAgg(Windows 上唯一可用的其他)。

    您还可以尝试更新spyder,然后尝试运行您的脚本。

    【讨论】:

    • 我更改了 Matplotlib 后端,但我仍然需要按两次运行。但是,脚本现在似乎在第二次按下运行后立即执行。
    • 这是github上描述的部分解决方案,我认为它会在下一个版本中修复。
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2018-12-25
    • 2022-08-19
    • 2021-10-09
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多