python version: 3.8.1
platform = Windows 10 pro
dev environment : visual studio code, jupyter notebook, command line
I have function that I import from a personal module to find all the current Pandas - DataFrames in memory or the globals(). However I only get the globals() from the module. (I know now that globals() only applies to the module it is called in) <--- This is my problem!
As stated above, I know this is not even remotely able to be done with the normal methodology. Here is my code from start to finish. **Note that I am calling my function from the module and this will only return "module globals()" not 'local globals()' from my current python instance's globals().
Code from "my_module" module:
# my_module.py #
import pandas as pd
module_dataframe = pd.Dataframe({'yourName'},columns = ['Name']) # creates dataframe in my module
def curDFs():
i ='' # create variable before trying to loop through globals() prevents errors when initiating the loop
dfList = []
for i in globals():
if str(type(globals()[i])) == "<class 'pandas.core.frame.DataFrame'>":
dfList.append(i)
return df_list
so you can see I am creating a Dataframe in my module and creating the function curDFs() to return the name of variables in globals() that are dataframes.
Below is the code from a brand new python session:
# new_window.py #
import my_module as mm
#not importing pandas since already imported into "my_module"
new_dataframe = mm.pd.DataFrame({'name'},columns = ['YourName'])
#calling globals() here will return the "local global variable"->"new_dataframe" created above
#if i call my "curDFs()" function from the module, I return not 'new_dataframe' but the 'module_dataframe' from the module
mm.curDFs()
#returns
['module_dataframe']
I need this to return only the "new_dataframe" variable. How would I do this? I am so stuck, every other post just goes over global and local scope or how to create a config.py file of global variables. However this is going to have to be dynamic and not static as I am seeing the congig.py file to be.
I think it would be a hassle to have to build this function every single instance I spin up. I want to be able import it so I can share with others or minimize the repetitive typing caused by having to copy paste or retype in every new python instance.
Any comments or help here is much appreciated.