python – Viewing all defined variables

python – Viewing all defined variables

A few things you could use:

  • dir() will give you the list of in scope variables:
  • globals() will give you a dictionary of global variables
  • locals() will give you a dictionary of local variables

If possible, you may want to use IPython.

To get a list of all current user-defined variables, IPython provides a magic command named who (magics must be prefixed with the % character unless the automagic feature is enabled):

In [1]: foo = bar
In [2]: %who

You can use the whos magic to get more detail:

In [3]: %whos
Variable   Type    Data/Info
foo        str     bar

There are a wealth of other magics available. IPython is basically the Python interpreter on steroids. One convenient magic is store, which lets you save variables between sessions (using pickle).

Note: I am not associated with IPython Dev – just a satisfied user.


You can find all the magic commands in the IPython Documentation.

This article also has a helpful section on the use of magic commands in Jupyter Notebook

python – Viewing all defined variables

To get the names:

for name in vars().keys():

To get the values:

for value in vars().values():

vars() also takes an optional argument to find out which vars are defined within an object itself.

Leave a Reply

Your email address will not be published. Required fields are marked *