0

I want to fetch all the variables and objects that were created in a python program with their types after it has finished execution. Suppose I have the following Python Script :

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from configparser import ConfigParser

di = {'a':1,'b':2,'c':3}

df = pd.DataFrame(di)

config = ConfigParser()

min_max = MinMaxScaler()

new_features = []

Note : This script doesn't make any sense but has just been written for demonstration.

After this script has executed, we should get the following output. The output maybe in the form of a dictionary which can later be converted into a dataframe.

Variables Type
di dict
df DataFrame
config ConfigParser
min_max MinMaxScaler
new_features list

While this can be done using the IPython magic command "whos" but it has limitation that it can only be used in Jupyter or IPython cell. I want a method that is independent of any platform.

Also, I don't want to add/modify anything to this file to get the output. Any function that can solve the problem should be run externally in a different python file.

Rishav Chetan
  • 141
  • 1
  • 1
  • 6
  • 1
    check out the python AST library, that should be the perfect solution to your current challenge – maestro.inc Apr 27 '21 at 11:12
  • @maestro.inc Could you help me with any particular function that would solve the requirement? – Rishav Chetan Apr 27 '21 at 11:16
  • 1
    Does this answer your question? [How to get the list of all initialized objects and function definitions alive in python?](https://stackoverflow.com/questions/4458701/how-to-get-the-list-of-all-initialized-objects-and-function-definitions-alive-in) – paradocslover Apr 27 '21 at 11:19
  • @paradocslover I will have to call the functions locals() or globals() in the same file to get the output. Is there any way such that I can get the required output by calling these functions externally? – Rishav Chetan Apr 27 '21 at 11:35

0 Answers0