Forum Archive

run script from console / commandline

robopu

simple question. i wrote a script in editorial console and it works fine there. pasted it to pythonista and it runs when i hit the play button.

but i want to execute it from the cmdline so i can redirect output to a file.

i'm trying :

python script.py

but there is no love. script.py lives in the root directory of the pythonista library.. it appears the console is trying to interpret script.py as a sting.. sorry if it's simple question but a dumb answer but the documentation told me the above is what i'm supposed to be running.. i can't give the file perms and i'm not sure i need to include an interpreter comment line up at the top of the file as if i was in linux...

thanks

robopu

well if i just type 'python'.. it thinks that's a string too ;).. so is the documentation wrong? it looks like i'm obviously already in the interpretter and it wants python language statements issued here individually

fwiw, i would like to write print output from script to a file using redirection. it's easier if i can run the script form the cmdline vs having to code it up

Webmaster4o

Pythonista does not provide a bash console. The console is a Python interpreter. You can try reading sys.stdout and writing its contents to a file.

robopu

sidenote: quick question with my script. in editorial it didn't mind if the print statements weren't wrapped in ().. but pythonista is demanding it meaning i have to go and change all my print statements. would prefer not to have to do that.. there are lots of them.. is there a way to just disable this somehow in this script?.. i'll be sure to use () in going forward ;)

robopu

@Webmaster4o

ok.. but i could swear the documentation just showed me an example of running a script using:

python script.py

since this is pythonista documentation.. i figured they meant it when they said it ;) even if i can't use redirect per bash, can i still invoke a script per above (though i guess it doesn't give me much advantage other than that i don't have to load the actual script in the editor to run it)?

thx

Webmaster4o

It's a difference between Python 2 and Python 3. Editorial is still on Python 2. While Pythonista supports both, 3 is the newer version and the default.

If you'd like to use Python 2 as the default for compatibility with Editorial, check out the "default interpreter" option on the settings.

Webmaster4o

@robopu You're probably reading the official documentation from the Python Software Foundation, which targets desktop computers over Pythonista. Everything about the Python language will be the same, but the other stuff may be a little different. So no, you can't invoke a Python script like that.

robopu

ok thx for both those replies webmaster.. makes sense..

ywangd

This is the use case that StaSh was initially created for. You can invoke your script from within StaSh similar to what you would do on a PC. You can redirect the output to a file as the follows:

your_script.py > output.txt

More details here https://github.com/ywangd/stash
If you have previous experience with Linux shell, things should be quite familiar.