35  Python: Debug using Spyder

Debugging Python functions are very similar although different IDEs have slightly different setups.

In this page, we show examples using Spyder. Generally, you can debug directly from both IDE environment.

35.1 Python script

Code
import numpy as np

def fn_n(x, **kwargs):
    na_rm = kwargs.get('na_rm', None)
    if na_rm:
          n = len(x[~np.isnan(x)])
    else:
          n = len(x)
    return(n)

def fn_sum(x, **kwargs):
    na_rm = kwargs.get('na_rm', None)
    if na_rm:
          out = np.nansum(x)
    else:
          out = np.sum(x)
    return(out)

def fn_prod(x, **kwargs):
    na_rm = kwargs.get('na_rm', None)
    if na_rm:
          out = np.nanprod(x)
    else:
          out = np.prod(x)
    return(out)
 
def fn_suminv(x, **kwargs):
    na_rm = kwargs.get('na_rm', None)
    if na_rm:
          out = np.nansum(1/x)
    else:
          out = np.sum(1/x)
    return(out)

def fn_AM(x, **kwargs):
  out = fn_sum(x, **kwargs)/fn_n(x, **kwargs)
  return(out)

def fn_GM(x, **kwargs):
  out = fn_prod(x, **kwargs)**(1/fn_n(x, **kwargs))
  return(out)

def fn_HM(x, **kwargs):
  out = fn_n(x, **kwargs)/fn_suminv(x, **kwargs)
  return(out)

# fn_nMean
def fn_Mean(x, **kwargs):
  n = fn_n(x, **kwargs)
  AM = fn_AM(x, **kwargs)
  GM = fn_GM(x, **kwargs)
  HM = fn_HM(x, **kwargs)
  Mean = {'n': n, 'AM': AM, 'GM': GM, 'HM': HM}
  return(Mean)


# Example

A = np.array([np.nan, 12, 15, 14, 18])

result = fn_Mean(A, na_rm = True)
print(result)

result = fn_Mean(A)
print(result)
{'n': 4, 'AM': 14.75, 'GM': 14.593795674586724, 'HM': 14.441260744985673}
{'n': 5, 'AM': nan, 'GM': nan, 'HM': nan}

35.2 Debugging directly using Spyder

  • Go to the Python script file with function

  • Set the breakpoint

  • Call the function under Debug environment (CTRL+F5 or Command+F)

35.3 Spyder Environment with breakpoint


35.4 Spyder Environment Debug Menu


35.5 Spyder Environment under Debugging Mode