Search code examples
pythonvariablesspyderlocal-variablesinspect

Maximise the f_locals dictionary in spyder variable explorer


This is a Spyder IDE specific question

Spyder IDE does not show local variables of a method in variable explorer, it only shows the global variables. So to get the local variables in variable explorer I use the inspect package in python. Code looks similar to below, program code is rather long and has dataframes, file pointers, Excelwriters and connection object to relational database.

local_vars = {}

def databases():
     global local_vars
     #program codes
     local_vars = inspect.currentframe().f_locals

On double clicking variables in variable explorer we would be able to view the variable in a seperate window as in the image variable view

but this is not working with the local_vars dictionary enter image description here

How can I remedy this and see local_vars in a seperate window ?


Solution

  • The best way to know the values of local variables is declaring a global dictionary to store the values

    var = {}
    
    def method():
        var1 = 2
        var2 = 3
        global var
        var['var1'] = var1
        var['var2'] = var2
    
    var1 = var['var1']
    var2 = var['var2']
    

    This method allows the local variables to be present in variable explorer