How to get a list of variables in specific Python module?

Question:

Let’s assume I have the following file structure:

data.py

foo = []
bar = []
abc = "def"

core.py

import data
# do something here #
# a = ...
print a
# ['foo', 'bar', 'abc']

I need to get all the variables defined in data.py file. How can I achieve that? I could use dir(), but it returns all the attributes of the module including __name__ and so on.

Asked By: aemdy

||

Answers:

Try:

for vars in dir():
 if vars.startswith("var"):
   print vars
Answered By: Arsh Singh
print [item for item in dir(adfix) if not item.startswith("__")]

Is usually the recipe for doing this, but it begs the question.

Why?

Answered By: Jakob Bowyer
#!/usr/local/bin/python
# coding: utf-8
__author__ = 'spouk'

def get_book_variable_module_name(module_name):
    module = globals().get(module_name, None)
    book = {}
    if module:
        book = {key: value for key, value in module.__dict__.iteritems() if not (key.startswith('__') or key.startswith('_'))}
    return book

import config

book = get_book_variable_module_name('config')
for key, value in book.iteritems():
    print "{:<30}{:<100}".format(key, value)

example config

#!/usr/local/bin/python
# coding: utf-8
__author__ = 'spouk'

import os

_basedir = os.path.abspath(os.path.dirname(__file__))

# database section MYSQL section
DBHOST = 'localhost'
DBNAME = 'simple_domain'
DBPORT = 3306
DBUSER = 'root'
DBPASS = 'root'

# global section
DEBUG = True
HOSTNAME = 'simpledomain.com'
HOST = '0.0.0.0'
PORT = 3000
ADMINS = frozenset(['admin@localhost'])
SECRET_KEY = 'dfg45DFcx4rty'
CSRF_ENABLED = True
CSRF_SESSION_KEY = "simplekey"

result function

/usr/local/bin/python2 /home/spouk/develop/python/2015/utils_2015/parse_config_py.py
DBPORT                        3306                                                                                                
os                            <module 'os' from '/usr/local/lib/python2.7/os.pyc'>                                                
DBHOST                        localhost                                                                                           
HOSTNAME                      simpledomain.com                                                                                    
HOST                          0.0.0.0                                                                                             
DBPASS                        root                                                                                                
PORT                          3000                                                                                                
ADMINS                        frozenset(['admin@localhost'])                                                                      
CSRF_SESSION_KEY              simplekey                                                                                           
DEBUG                         1                                                                                                   
DBUSER                        root                                                                                                
SECRET_KEY                    dfg45DFcx4rty                                                                                       
CSRF_ENABLED                  1                                                                                                   
DBNAME                        simple_domain                                                                                       

Process finished with exit code 0

Enjoy, dude. 🙂

Answered By: Spouk

This is the version I wrote for python 3.7 (it excludes the internal dunder methods via the condition in the comprehension)

print([v for v in dir(data) if v[:2] != "__"])

A longer but complete working example is below:

"""an example of a config file whose variables may be accessed externally"""
# Module variables
server_address = "172.217.167.68"
server_port = 8010
server_to_client_port = 8020
client_to_server_port = 8030
client_buffer_length = 4096
server_buffer_length = 2048

def printVariables(variable_names):
    """Renders variables and their values on the terminal."""
    max_name_len = max([len(k) for k in variable_names])
    max_val_len = max([len(str(globals()[k])) for k in variable_names])

    for k in variable_names:
        print(f'  {k:<{max_name_len}}:  {globals()[k]:>{max_val_len}}')

if __name__ == "__main__":
    print(__doc__)
    ks = [k for k in dir() if (k[:2] != "__" and not callable(globals()[k]))]
    printVariables(ks)

The above code outputs:

an example of a config file whose variables may be accessed externally
  client_buffer_length :            4096
  client_to_server_port:            8030
  server_address       :  172.217.167.68
  server_buffer_length :            2048
  server_port          :            8010
  server_to_client_port:            8020
Answered By: John Forbes

I have to make a dictionary of these variables. I used this code.

print({item:getattr(my_module, item) for item in dir(my_module) if not item.startswith("__") and not item.endswith("__")})
Answered By: Sheikh Abdul Wahid

I offer my solution. It is convenient in that it allows you to display variables from any imported module.

If you do not specify the name of the module, then the list of variables of the current module is displayed.

import sys

def print_settings(module_name=None):
    module_name = sys.modules[__name__] if not module_name else module_name
    variables = [
        (key, value)
        for (key, value) in vars(module_name).items()
        if (type(value) == str or type(value) == int or type(value) == float)
        and not key.startswith("_")
    ]

    for (key, value) in variables:
        print(f"{key: <20}  {value}")
Answered By: Maxim Deviatov

Here’s a roundabout way, if you prefer to be more explicit:

data.py

a = [
  foo := [],
  bar := [],
  abc := "def",
]

core.py

import data

print(data.foo)
print(data.a)
Answered By: Lucia

If you need the variable and the value assigned to it then

import data

for name ,values in vars(data).items():
    print(name, values)

You can choose to store name (all the variable names in the script) or the value attached to it .

Answered By: suraj
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.