[Tutor] how to pass arguments to script
Alan Gauld
alan.gauld at btinternet.com
Mon Sep 9 04:47:49 EDT 2019
On 08/09/2019 23:16, Anil Felipe Duggirala wrote:
> I am currently completing the Python tutorial, in python.org.
> I wonder, if I write a script.py defining a function, and the function
> requires an argument to produce a result; how do I specify this
> argument when executing script.py in my console?
Take a look at the "Talking to the User" topic in my tutorial(link below)
About 3/4 way down there is a section "Command Line Parameters"
that describes how to do what you want.
Or read the documentation on sys.stdin
> Is there a way to run the script in interactive mode and also quickly
> pass the function's argument?
You can't easily run scripts at the interactive prompt (ie >>>). You can
import a module and if your script is written such that you can execute
it by calling a function then that's the simplest option.
This usually means having your script code look like:
######## MyModule.py ######
import sys
def something:....
def another......
def main(arg):
# my main code here
if __name__ == "__main__":
args = sys.argv
main(args[1])
####################
Then you
>>> import MyModule
>>> MyModule.main(myArg)
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list