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

2
  • 2
    why are you using globals? Commented Apr 7, 2021 at 18:16
  • Python doesn't have global global variables. We only have module-level dictionaries, which is what is returned by globals Commented Apr 7, 2021 at 18:18

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.