How to return a value from a script running inside another script?

I’d like to get the opinion of the Python gurus as to the correct way to handle this. I think I might be over-complicating it.

Say I have Script A that returns a value. Could be a boolean, could be a numeric value, whatever. I want to run Script A inside Script B, but I want that value to be passed from Script A to Script B when Script B runs.

What is the correct Pythonese way of handling this?

Thanks,

Dan

Something like this?

script_a.py

def return_a_string():
    return 'hi there.'

script_b.py

import script_a

def use_script_a():
    print script_a.return_a_string()

use_script_a()

Thanks for the response.

I gave your code a try and it worked. Except… if I change the text in script_a it still returns as the old text in script_b.

Any suggestions?

Thanks,

Dan

I think you’ll need to reset the script engine (in the Tools menu of the python editor).

Wasn’t there something about “reload modules” instead of resetting the script engine? ISTR something like that, but I never use scripts in this way, so no personal experience…

Yeah, looks like this works:

import script_a
reload(script_a)
...

Thanks. That worked.

def use_script_a():
    import script_a
    print script_a.return_a_string()

n = 23
fact = 1
for i in range(1,n+1): 
 import factorial0
 fact = factorial0.factorial(fact,i) 
      
print ("The factorial of 23 is : ",end="") 
print (fact)

factorial0.py

def factorial(fact,i):
   return fact * i
1 Like

This was very helpful.

Thanks,

Dan

Almost, but not quite…

The script runs fine as a script, but as soon as I compile it into my plug-in, it fails. I think I could probably avoid a lot of grief by just making the first script a function in the second and not try to link them.

But just for my own education I’m open to any ideas as to why the compiled script fails. BTW… I followed Javier’s example which works well as script.

Thanks,

Dan

It depends what you mean by saying it fails. For me, Javier’s code did not solve the problem of running stale code, only an explicit call to reload(script_a) did. But if you mean some other general failure, then I guess it would be related to how compiled scripts are unpacked for running.

As for folding both functions into a single script, that sounds like a fine idea, if there is not a compelling reason to have them separated – maybe your project is complicated enough to justify the time spent to make it work (provided there is not some technical reason it can’t), but otherwise, the value of minimizing dependencies should also not be underestimated.

1 Like

Hi @DanBayn,

that is likely because the RhinoScriptCompiler does not support modules. It would be great though.
_
c.

1 Like

I’ve put it all together in one script and it’s working well. My initial idea was to take an existing script that was already in use and have it run as a function of another. But taking the easy way out didn’t work out so well. I appreciate your help.

Thanks,

Dan

Hi Clement,

That would explain it. Live and learn I guess.

Thanks,

Dan