Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Emulate run button - run script from another script
-
Thanks to both of you. I realized this immediately after I did post this question. Anyway, it makes things little bit harder, because it forces me to make Black Mamba Python 2/3 compatible. But it will not probably solve my issue anyway, because I can't run Python 2 scripts from Python 3 and vice versa. Would be nice if editor (or any other module) has function like
run_script
which can issuerunpy.run_path
under correct interpreter, based on shebang line. -
I think @JonB (or someone else?) had success using the Python C API via
ctypes
for this kind of thing, not sure… -
If anyone wonders why I need this, check this comment in another thread. This is the last missing piece to make me happy :)
-
I solved it with URL scheme for now, but it's kinda limited. Anyway I'm interested in this Python C API hack to choose interpreter, ...
-
This thread might be useful. Things have changed a bit in recent versions, so i update my original discoveries below:
If you are trying to emulate play button, it eventually calls:
I3=ObjCClass('PYK3Interpreter').sharedInterpreter() I2=ObjCClass('PYK2Interpreter').sharedInterpreter() #pick which interpreter to run (I2 vs I3) and whether to reset environment I2.runScriptAtPath_argv_resetEnvironment_('/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/Untitled_150.py',['1', 'hello'], True)
(Wrench menu calls the same selector, just with resetEnvironment=False, and populating first argv with editor script name )
I believe there might be a limitation that if a script is running already, it might be ignored, but i am not sure if that is still true. Polling for
running()
to return 0 would work, if that was the case.Previously, a script had to be open in the editor, but that is no longer the case it seems.
For running python commands directly via the cpythonapi, see the above thread.
-
btw, if you go the runpy/cpythonapi route to try to run concurrently,
ObjCClass('PA3InterpreterWrapper').shebangVersionForScript_()
might be helpful to tell you which version to run. Then load proper cpythonapi (see the stash thread), then to emulate pythonista evironment reset, you could run the preflight script:
import os version =2 #or 3 preflight_script=os.path.join(os.path.split(os.__file__)[0],'../../Py{}Kit.framework/pykit_preflight.py'.format(version))
-
@JonB said:
I believe there might be a limitation that if a script is running already, it might be ignored, but i am not sure if that is still true. Polling for running() to return 0 would work, if that was the case.
You could in principle use the other interpreter like this, but trying to run a script in the current interpreter with this high-level API isn't possible.
-
@JonB thanks! Will try this.
-
@zrzka I don't think you'll get very far with this approach, to be honest.
-
@omz learn or die :) At least I'll learn more about Pythonista internals.