Tricksy request here…
So a few things. Let’s say you have an external script / function:
[code]import sys
here’s your function
def HelloWorld():
# note that we need the return to be a string
return ‘12’
here you call your function and pass the results
to stdout.write()
sys.stdout.write(HelloWorld())[/code]
Notice that you need to use sys.stdout.write() to move your results into touch / another process. You’ll also notice that you need to encode your results as a string.
In touch you’ll end up with something like:
[code]import subprocess
this is the location of your script
SCRIPT = ‘{}/start-py.py’.format(project.folder)
here you call your subprocess call, you need to pass
python as an argument for what’s opening the file
as well as the file you want to open with python
you’ll capture the output of
val = subprocess.Popen([‘python’, SCRIPT], stdout=subprocess.PIPE, shell=True)
this is the raw value
print(val.communicate()[0])[/code]
Subprocess is used to execute a process in the windows shell - you need to pass out the application you expect to open the file, and then the path to the file. The results can be retrieved with .communicate() - though you’ll want the first item in the tuple.
You’ll notice that those results are prepended with a ‘b’ which tells you that the results are in bytes. To get the decoded val you ll need to do something like:
import subprocess
# this is the location of your script
SCRIPT = '{}/start-py.py'.format(project.folder)
# here you call your subprocess call, you need to pass
# python as an argument for what's opening the file
# as well as the file you want to open with python
# you'll capture the output of
val = subprocess.Popen(['python', SCRIPT], stdout=subprocess.PIPE, shell=True)
# the 'b' here represents the fact that the data
# is included in bytes, to strip this down to the
# contents of the return we need to decode the results
print(val.communicate()[0].decode('utf-8'))[/code]
Here’s an example file with external script to help pull apart what’s happening:
subprocess.zip (4.54 KB)