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.
Simple UI to run script2 + args
-
Can anybody point me in the right direction?
I'm hoping to find a basic template or example.I need a script1.py to run "script2.py arg1 arg2 arg3"
I'm new to Pythonista, and not sure where to start.
Thank you! -
Hi,
welcome to Python(ista)! You could use
exec
(see here) to do what you have described. But usingexec
is usally considered really bad style for a reason as it WILL open the doors to purgatory at some point. Is there any reason why you could not do what you want to do with a normal module? -
Thank you. This is a start. But I also need some way to append user input as arguments for script2.
Essentially, a GUI wrapper for a pre-existing commandline .py -
You could just modify
sys.argv
in the first script, then userunpy.run_path
to run the second. -
This post is deleted! -
@omz Thank you. I think I have a start now, thanks to coming across this ex13-raw_input.py
-
@omz I've got this working based on the ex13 script
from sys import argv script2 = argv first_name = raw_input("What is your first name? ") middle_name = raw_input("What is your middle name? ") last_name = raw_input("What is your last name? ") print "Your full name is %s %s %s." % (first_name, middle_name, last_name)
But I have not got runpy to cooperate.
And this doesn't use the 'raw_input' which I need.Do you have a solution? Thank you for your help
-
import sys import runpy try: # https://docs.python.org/3/whatsnew/3.0.html#builtins raw_input # Python 2 except NameError: raw_input = input # Python 3 script2 = sys.argv[1] if sys.argv[1:] else 'script2.py' first_name = raw_input("What is your first name? ").strip() or 'Donald' middle_name = raw_input("What is your middle name? ").strip() or 'J.' last_name = raw_input("What is your last name? ").strip() or 'Trump' fmt = "%s: Your full name is %s %s %s." print(fmt % (sys.argv[0].split('/')[-1], first_name, middle_name, last_name)) runpy.run_path(script2, init_globals={'first_name': first_name, 'middle_name': middle_name, 'last_name': last_name}) """script2.py import sys fmt = "{}: Your full name is {first_name} {middle_name} {last_name}." print(fmt.format(sys.argv[0], **globals())) """
-
@ccc Thank you for the thoughtful answer!
I think this discussion has strayed away from my original intention.
To rephrase my original intention:
A have a command line Python script which would be used as follows "python main.py arg1 arg2 arg" and then proceeds to do and print some predefined functions.
Now, I want use a wrapper script to run main.py with the args input via a prompt, and print what main.py is doing.Thank you for help!
-
import sys import runpy try: # https://docs.python.org/3/whatsnew/3.0.html#builtins raw_input # Python 2 except NameError: raw_input = input # Python 3 script2 = sys.argv[1] if sys.argv[1:] else 'script2.py' first_name = raw_input("What is your first name? ").strip() or 'Donald' middle_name = raw_input("What is your middle name? ").strip() or 'J.' last_name = raw_input("What is your last name? ").strip() or 'Trump' fmt = "%s: Your full name is %s %s %s." print(fmt % (sys.argv[0].split('/')[-1], first_name, middle_name, last_name)) sys.argv = ['script2.py', first_name, middle_name, last_name] with open(sys.argv[0]) as in_file: exec(in_file.read()) # exec() is a security hole a mile wide and 10 miles deep!! """script2.py import sys print("{}: Your full name is {} {} {}.".format(*sys.argv)) """
-
Python 3 fix also merged into upstream... https://github.com/mwarkentin/Learn-Python-The-Hard-Way/pull/2/files
-
Uh, I didn't know that python had a short hand syntax for conditional assignments evaluating for
True
. That is nice 👍🏻. -
rich,
Are you aware that you long-press the play button for a script, it lets you define arguments? Just checking, not everyone realizes this.another option would be to use a
dialogs
popup rather than raw_inputimport dialogs fields=[{'title':'First Name','type':'text'}, {'title':'Last Name','type':'text'}, {'title':'Age','type':'number'}] response=dialogs.form_dialog('What is your info?',fields)
-
@JonB Thanks. I really like this dialogue box! If I can get the responses to act as args to control my second script, then I have it made!
-
This post is deleted! -
Put that text at the end into a file called
script2.py
;-). Then run the rest of the code in a separate file that I calledscript1.py
. -
@ccc Thanks. I still get the traceback error "AttributeError: 'tuple' object has no attribute 'remove'"
-
Try changing this line:
sys.argv = ['script2.py', first_name, middle_name, last_name]
To have square brackets [] instead of parentheses ().
-
This post is deleted! -
@ccc Thank you! This works as expected!
The first two args are necessary, the third arg is supposed to be optional. In its current form, (with Trump removed of course,) leaving the third arg blank causes script2 error (citing an invalid arg).
Do you know how to leave an arg as optional?