Rhino python script bug when imported as module

I had a really weird experience of getting different results when calling a function from an imported module (which is just another script I wrote) and when calling the same function after copying and pasting the function to the main script.

So for example, I have in script1.py:

def testFunction(input):
    blah blah
    return blah

if __name__ == '__main__':
    blah = testFunction(input)

Then in script2.py:

import script1
script1 = reload(script1)

blah = script1.testFunction(input)

I thought these two should return the exact same results, which is true for small scripts. But when I was trying to run a pretty big script this way, most parts of the script ran as I expected, but part of the script didn’t return the expected results. I also tried to add breakpoints and tested it a few times, there was just one time that it ran correctly when I call the function in another script, but the other times they all had the wrong results. I am completely confused! There were no errors raised and to the best of my ability, I could guess that maybe it has to do with something not yet finished calculated when the next line started running, but it still didn’t make much sense. I for now just put everything in one script and it worked without any problem.

I’m just wondering if anyone else had encountered the same problem and/or knows why that happened. If you want to test it out for yourself, feel free to send me your email address so I could send you the scripts and the model.

Thanks in advance!

Hi Rollerpaw,

This is not expected and likely something else is at play
Why do you use the reload()?
Are you running from the
Do you switch files during the script?
Could it be some variables are named the same in the separate files?
Can you provide a simple example to reproduce it?