0

There is a module in helpers directory called helper_a.py. It has all classes and functions defined here.

Now I want to call a function from here into another module (not in helpers directory) but one step (cd ..) behind. (init.py) is in helpers directory.

Code and error is as below :

   from helpers.helper_a import *
   import pandas as pd

   query_train_data = "select * from train;"
   df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)

   query_test_data = "select * from test;"
   df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)

   df_train_data = df_train_dataset
   df_test_data = df_test_dataset

   data_prep_steps() # This function is defined in helpers_a


   Error:

    ---------------------------------------------------------------------------
   NameError                                 Traceback (most recent call last)
   <ipython-input-12-3c88b46f341a> in <module>
   ----> 1 data_prep_steps()

   ~\Desktop\Notebooks\helpers\helper_a.py in data_prep_steps()
   ---> 89     # STEP 1 : CONVERT REQUIRED COLS TO CATEGORIES
        90     for df_name in [df_train_data, df_test_data]:
        91         data_prep_class = data_prep(df_name)

NameError: name 'df_train_data' is not defined

Question is that the variable df_train data is defined in the current module and i want to use it in the function defined in helpers_a by calling it also in the current module, but why is it not recognizing this variable??

Note : Also tried assigning global variable status but it still doesnt solve the issue

2
  • I suspect this stacktrace is not the original one, or is incomplete. It does not match the code above. If you need df_train_data and df_test_data into data_prep_steps(), why don't you pass these 2 variables as arguments ? -- I think you should try to make a minimal reproducible example, the questions will be more easily answered this way, and by reproducing your example, you might find the answer yourself. Commented Jul 25, 2019 at 12:03
  • It is the original stack trace, i have added the complete one. Not sure what makes it feel incomplete ( edited now to add complete) Commented Jul 25, 2019 at 12:09

1 Answer 1

1

There is a solution to create non existing attributes,methods or functions in other modules. It comes from unit testing.


from unittest.mock import patch, PropertyMock

from helpers.helper_a import *
import pandas as pd

query_train_data = "select * from train;"
df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)

query_test_data = "select * from test;"
df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)

df_train_data = df_train_dataset
df_test_data = df_test_dataset

with patch('helpers.helper_a.df_test_data',create=True,new_callable=PropertyMock) as df_test_data_mock: #Create tells to create attribute if it does not exist
    with patch('helpers.helper_a.df_train_data', create=True, new_callable=PropertyMock) as df_train_data_mock:  # PropertyMock is used to mock properties 
        df_test_data_mock.return_value = df_test_data
        df_train_data_mock.return_value = df_train_data
        data_prep_steps()  # This function is defined in helpers_a

Although I agree with comments that passing those values would be way simpler. Also due to python dynamic nature you can just simply set those attributes on the module itself. This method is way simpler but you need to remember to clean up after your done which previous method does for you with context mananger.

   import helpers.helper_a
   import pandas as pd

   query_train_data = "select * from train;"
   df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)

   query_test_data = "select * from test;"
   df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)

   helpers.helper_a.df_train_data = df_train_dataset
   helpers.helper_a.df_test_data = df_test_dataset

   helpers.helper_a.data_prep_steps() # This function is defined in helpers_a
Sign up to request clarification or add additional context in comments.

5 Comments

I tried to passing in function as arguements, but a change in df in current module wont propogate to the helper module without restarting my workbook
@Viv well you should have pointed out you work in jupiter which is a specific work environment. But did the method provided by me did not work?
Your method works, argument way doesnt unless i force reload the defined module, upvoted your answer
@Viv Please mark then the answer as correct, glad I could help :)
@Viv Check out the edit as it is way simpler but may not work in all cases

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.