0

I have the below project structure in Linux.

main_project
├── base_dir
│   └── helper
│       └── file_helper.py
│       └── __init__.py
    └── jobs
        └── adhoc_job.py
        └── __init__.py

        └── test_job.py
        └── __init__.py     

In helper/file_helper.py script I have below block of code

def null_cols_exception(df, cols_to_check):
    """
    :param df: data frame on which the function has to be applied
    :param cols_to_check: columns to check
    :return:
    """
    for i in cols_to_check:
        excpt_cd = globals()[i + '_null_exception_code']
        print(excpt_cd) 
        
        # There is some logic here but for test purpose removed it
        # Here the data gets overwritten in LOOP I have covered that scenario but for test purpose removed it

        
        

Now I am using this function jobs/adhoc_job.py script

from helper.file_helper import null_cols_exception

test_col_null_exception_code = 123
test_col2_null_exception_code = 999

null_cols = ['test_col', 'test_col2']

# calling the null_cols_exception function
null_df = null_cols_exception(test_df, null_cols)

Now when I run the jobs/adhoc_job.py script

I am getting below error at the below point

        excpt_cd = globals()[i + '_null_exception_code']
    KeyError: 'test_col_null_exception_code'
    

Basically the function is unable to derive the test_col_null_exception_code variable

How can I resolve this issue

nmr
  • 605
  • 6
  • 20

1 Answers1

2

globals() only knows about the globals within the own module.

From the docs (emphasis mine):

Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

You'll have to pass the globals around if you want to go that way.

# calling the null_cols_exception function
null_df = null_cols_exception(None, null_cols, globals())

and

def null_cols_exception(df, cols_to_check, _globals):
    """
    :param df: data frame on which the function has to be applied
    :param cols_to_check: columns to check
    :param _globals: globals() from another module
    :return:
    """
    for i in cols_to_check:
        excpt_cd = _globals[i + '_null_exception_code']
        print(excpt_cd)

Output:

123
999

But you're probably better off by using a list/dict to hold your objects and then pass that around.

Reference: How do I create variable variables?

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50