0

I am new to Python and struggling with maybe a simple issue:

I have a main script called A_script.py. In it is the following code (simplified and anonymised):

if __name__ == '__main__':
    import pandas as pd
    df_A = pd.read_sql_query('SELECT * FROM [xxx].[xx].[xxxxx]', conn)
    A_len = len(df_A.index)
    
    #import script B_script.py
    import B_script

B_script contains the following code:

from A_script import df_A
from A_script import A_len

This code in B_script errors when I run the script A_script, as it cannot bring in the variable and the dataframe. How do I get around this, without having to rerun the entire A_script from within B_script that creates df_A and A_len, which is significantly larger and more complex than the example?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    it looks like you have a circular import. Script B depends on Script A, which depends on Script B, etc. If you need to run both scripts together, then I would suggest splitting out the "run" part into an independent script. That way A can be independent and B can depend on A, while your new script can depend on both. – PirateNinjas Jun 03 '21 at 08:25
  • 1
    I might be misunderstanding something, but when you run your A_script, somewhere along the line it imports the B_script which in it's turn imports some stuff from A_script?! – shcrela Jun 03 '21 at 08:28

1 Answers1

0

Two points.

First, you're doing a circular import. A_script is importing B_script and B_script is importing A_script. Change this. If both files depend on code from each other, then refactor, create a new file, and put the needed functions, and import from there.

Point number two, in A_script, your code is in an if clause if __name__ == '__main__'. When you import a module, __name__ is not __main__. It only is if the file is run directly. Check this SO post.

So I guess currently you're getting an error saying that df_A and A_len is not found in A_script. That's because that code is never run. So remove the if clause.

import pandas as pd
df_A = pd.read_sql_query('SELECT * FROM [xxx].[xx].[xxxxx]', conn)
A_len = len(df_A.index)
Safwan Samsudeen
  • 1,645
  • 1
  • 10
  • 25
  • Thanks! So A_script is not dependant on B_script, it is just importing the script to run B_script. B_script is dependant on variables and dfs from A_script. If I remove the if clause, does B_script rerun A_script, as that is not what I want to happen... – jl129301924245 Jun 03 '21 at 11:45
  • I don't understand. Why do you need to "run B_script"? And what do you mean by rerun A_script? – Safwan Samsudeen Jun 04 '21 at 09:50