List Python Variables

Last modified: 
Sunday, March 29th, 2015
Topics: 
Python

How to list all variables in a Python script

#! /usr/bin/env python
# @file getVars.py

# Set FTP connection variables.
dbname        = 'DBNAME';
username      = 'USERNAME'
password      = 'PASSWORD'
host          = 'LOCALHOST'


for name in dir():
    myvalue = eval(name)
    print name, "is", type(name), "and is equal to ", myvalue

Returns something like this (sorry, but one of my input filters is mangling this output)...

__builtins__ is  and is equal to  
__doc__ is  and is equal to  None
__file__ is  and is equal to  ./getVars.py
__name__ is  and is equal to  __main__
__package__ is  and is equal to  None
dbname is  and is equal to  DBNAME
host is  and is equal to  LOCALHOST
password is  and is equal to  PASSWORD
username is  and is equal to  USERNAME

!!References
Enumerate or list all variables in a program of {your favorite language here}


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.