Re: [Tutor] How to run a .py file or load a module?
Denis, this mail was very comprehensive, and went a long way of driving it all home for me. There are several different concepts that are involved in this simple problem that I had, and you guys explaining them has really expanded my pythonic horizon, especially the explanations on the argv module, and also the idea of from import as Thanks a lot, everybody. :-) Dayo -- spir wrote: Le Sun, 26 Apr 2009 22:35:36 +0100, Dayo Adewunmi s'exprima ainsi: How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. When your code is (nicely) organised as a set of funcs or class definitions, you also need a "laucher" usually called "main()". Otherwise python only parses and records the definitions into live objects that wait for someone to tell them what they're supposed to do. I'll stick first at processes without any parameter, like if your func would always countdown from 10. There are several use patterns: (1) Program launched from command line. Just add a call to your func: countdown(10) (2) Module imported from other prog Nothing to add to your module. Instead, the importing code needs to hold: import countdown # the module (file) ... countdown.countdown(n) # the func itself or from countdown import countdown # the func, directly ... countdown(n) (3) Both You need to differenciate between launching and importing. Python provides a rather esoteric idiom for that: if __name__ == "__main__": countdown(10) The trick is that when a prog is launched directly (as opposed to imported), it silently gets a '__name__' attribute that is automatically set to "__main__". So that the one-line block above will only run when the prog is launched, like in case (1). While nothing will happen when the module is imported -- instead the importing code will have the countdown func available under name 'countdown' as expected, like in case (2). Clear? b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. In the case of an import, as your func definition has the proper parameter, you have nothing to change. While for a launch from command-line, you need to get the parameter given by the user. But how? Python provides a way to read the command-line arguments under an attribute called 'argv' of the 'sys' module. argv is a list which zerost item is the name of the file. For instance if called python countdown.py 9 argv holds: ['countdown.py', '9'] Note that both are strings. Then you can catch and use the needed parameter, e.g. from time import sleep as wait from sys import argv as user_args def countdown(n=10): if n <= 0: print 'Blastoff!' else: wait(0.333) print n countdown(n-1) def launch(): if len(user_args) == 1: countdown() else: n = int(user_args[1]) countdown(n) if __name__ == "__main__": launch() (You can indeed put the content of launch() in the if block. But I find it clearer that way, and it happens to be a common practice.) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
David wrote: Norman Khine wrote: On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers wrote: Here is another one for fun, you run it like python countdown.py 10 #!/usr/bin/env python import sys from time import sleep times = int(sys.argv[1]) # The argument given on the command line def countdown(n): try: while n != 1: n = n-1 print n sleep(1) finally: print 'Blast Off!' countdown(times) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thank you all for all your valuable input on this. I have learned so much on this particular subject in such a short time. David, I ran your code, and noticed that given countdown(10) your countdown starts at 9 and Blastoff takes place after 1, not 0. To fix that, I changed while n ! = 1 to while n != 0 and changed n = n - 1 print n to print n n = n -1 Thanks for the time you guys have put into this. It's much appreciated. :-) Dayo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
"Alan Gauld" wrote Offlist response... Oops, not offlist! Just as well I didn't say anything offensive! :-) Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
Offlist response... Nice answer Denis, you neatly covered all the loose ends from previous answers that I was just about to pick up on! :-) "spir" wrote in message news:20090427101523.5fc09...@o... Le Sun, 26 Apr 2009 22:35:36 +0100, Dayo Adewunmi s'exprima ainsi: How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. When your code is (nicely) organised as a set of funcs or class definitions, you also need a "laucher" usually called "main()". Only question is why, after saying it's usually called "main" that you chose "launch"?! :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
Le Sun, 26 Apr 2009 22:35:36 +0100, Dayo Adewunmi s'exprima ainsi: > How can I > > a) Open my shell, and do something like: $ python countdown.py > but have it take an argument and pass it to the function, and execute. When your code is (nicely) organised as a set of funcs or class definitions, you also need a "laucher" usually called "main()". Otherwise python only parses and records the definitions into live objects that wait for someone to tell them what they're supposed to do. I'll stick first at processes without any parameter, like if your func would always countdown from 10. There are several use patterns: (1) Program launched from command line. Just add a call to your func: countdown(10) (2) Module imported from other prog Nothing to add to your module. Instead, the importing code needs to hold: import countdown # the module (file) ... countdown.countdown(n) # the func itself or from countdown import countdown # the func, directly ... countdown(n) (3) Both You need to differenciate between launching and importing. Python provides a rather esoteric idiom for that: if __name__ == "__main__": countdown(10) The trick is that when a prog is launched directly (as opposed to imported), it silently gets a '__name__' attribute that is automatically set to "__main__". So that the one-line block above will only run when the prog is launched, like in case (1). While nothing will happen when the module is imported -- instead the importing code will have the countdown func available under name 'countdown' as expected, like in case (2). Clear? > b) Import the function in the interactive interpreter, and call it like so: > > countdown(10) > > without getting the abovementioned error. In the case of an import, as your func definition has the proper parameter, you have nothing to change. While for a launch from command-line, you need to get the parameter given by the user. But how? Python provides a way to read the command-line arguments under an attribute called 'argv' of the 'sys' module. argv is a list which zerost item is the name of the file. For instance if called python countdown.py 9 argv holds: ['countdown.py', '9'] Note that both are strings. Then you can catch and use the needed parameter, e.g. from time import sleep as wait from sys import argv as user_args def countdown(n=10): if n <= 0: print 'Blastoff!' else: wait(0.333) print n countdown(n-1) def launch(): if len(user_args) == 1: countdown() else: n = int(user_args[1]) countdown(n) if __name__ == "__main__": launch() (You can indeed put the content of launch() in the if block. But I find it clearer that way, and it happens to be a common practice.) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
Norman Khine wrote: On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers wrote: Here is another one for fun, you run it like python countdown.py 10 #!/usr/bin/env python import sys from time import sleep times = int(sys.argv[1]) # The argument given on the command line def countdown(n): try: while n != 1: n = n-1 print n sleep(1) finally: print 'Blast Off!' countdown(times) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers wrote: > 2009/4/26 Dayo Adewunmi : >> I'm looking at recursion in "Think Python", and this is the bit of code: >> >> #!/usr/bin/env python >> >> def countdown(n): >> if n <= 0: >> print 'Blastoff!' >> else: print n >> countdown(n-1) >> >> >> I've typed that in vim and saved as countdown.py, but I'm not sure how to >> run it. >> >> However with this particular function that requires an argument, I'm not >> sure how to run it. >> >> I've had to type it out in the python prompt and then >> call >> the function with an argument. That works, naturally. >> >> I've also tried this: >> >> >>>import countdown >> >>>countdown(10) > > When you import it lile this the function countdown is part of the > module countdown. So you call it like countdown.countdown(10). Or > import it like "from countdown import countdown" and then your example > will work. > >> but this is the error I get: >> >> Traceback (most recent call last): >> File "", line 1, in >> NameError: name 'countdown' is not defined >> >> How can I >> >> a) Open my shell, and do something like: $ python countdown.py but have it >> take an argument and pass it to the function, and execute. > > Look at sys.argv which returns a list with the first value being the > script name and the second are the command line argument(s). > http://docs.python.org/library/sys.html > >> b) Import the function in the interactive interpreter, and call it like so: >> >> countdown(10) >> >> without getting the abovementioned error. > > See above. > > The script would then look like: > > #!/usr/bin/env python > > import sys > > times = int(sys.argv[1]) # The argument given on the command line > > def countdown(n): > if n <=0: > print 'Blast off!' > else: > countdown(n-1) > > countdown(times) Don't forget to add: if __name__ == '__main__': launchme(times) > > Greets > Sander > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
2009/4/26 Dayo Adewunmi : > I'm looking at recursion in "Think Python", and this is the bit of code: > > #!/usr/bin/env python > > def countdown(n): > if n <= 0: > print 'Blastoff!' > else: print n > countdown(n-1) > > > I've typed that in vim and saved as countdown.py, but I'm not sure how to > run it. > > However with this particular function that requires an argument, I'm not > sure how to run it. > > I've had to type it out in the python prompt and then > call > the function with an argument. That works, naturally. > > I've also tried this: > > >>>import countdown > >>>countdown(10) When you import it lile this the function countdown is part of the module countdown. So you call it like countdown.countdown(10). Or import it like "from countdown import countdown" and then your example will work. > but this is the error I get: > > Traceback (most recent call last): > File "", line 1, in > NameError: name 'countdown' is not defined > > How can I > > a) Open my shell, and do something like: $ python countdown.py but have it > take an argument and pass it to the function, and execute. Look at sys.argv which returns a list with the first value being the script name and the second are the command line argument(s). http://docs.python.org/library/sys.html > b) Import the function in the interactive interpreter, and call it like so: > > countdown(10) > > without getting the abovementioned error. See above. The script would then look like: #!/usr/bin/env python import sys times = int(sys.argv[1]) # The argument given on the command line def countdown(n): if n <=0: print 'Blast off!' else: countdown(n-1) countdown(times) Greets Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
Dayo, I modified the code a little bit to make things work the way I think you meant it to work(hopefully), and I changed the name of the function so that its' not the same name as the python file itself, but hopefully this answers your questions. Here is my countdown.py def launchme(n): while n > 0: print n n -= 1 else: print 'Blastoff!' #uncomment to run from the shell #launchme(7) So, assuming we're running the interpreter from the same folder that countdown.py is in. You have to call module.function(parameter) >>> import countdown >>> countdown.launchme(4) 4 3 2 1 Blastoff! >>> If we uncomment the "#launchme(7)" line, and run it from the shell: $ python countdown.py 7 6 5 4 3 2 1 Blastoff! On Sun, Apr 26, 2009 at 3:35 PM, Dayo Adewunmi wrote: > I'm looking at recursion in "Think Python", and this is the bit of code: > > > #!/usr/bin/env python > > def countdown(n): > if n <= 0: > print 'Blastoff!' > else: print n > countdown(n-1) > > > I've typed that in vim and saved as countdown.py, but I'm not sure how to > run it. I've had other > python files, where the triggering function didn't take any arguments, > so I would just put a `foo()` at the end of the .py file. > > However with this particular function that requires an argument, I'm not > sure how to run it. I've had to type it out in the python prompt and then > call > the function with an argument. That works, naturally. > > I've also tried this: > > >>>import countdown > >>>countdown(10) > > but this is the error I get: > > Traceback (most recent call last): > File "", line 1, in > NameError: name 'countdown' is not defined > > How can I > > a) Open my shell, and do something like: $ python countdown.py but have > it take an argument and pass it to the function, and execute. > > b) Import the function in the interactive interpreter, and call it like so: > > countdown(10) > > without getting the abovementioned error. > Thanks. > > Dayo > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to run a .py file or load a module?
I'm looking at recursion in "Think Python", and this is the bit of code: #!/usr/bin/env python def countdown(n): if n <= 0: print 'Blastoff!' else: print n countdown(n-1) I've typed that in vim and saved as countdown.py, but I'm not sure how to run it. I've had other python files, where the triggering function didn't take any arguments, so I would just put a `foo()` at the end of the .py file. However with this particular function that requires an argument, I'm not sure how to run it. I've had to type it out in the python prompt and then call the function with an argument. That works, naturally. I've also tried this: >>>import countdown >>>countdown(10) but this is the error I get: Traceback (most recent call last): File "", line 1, in NameError: name 'countdown' is not defined How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. Thanks. Dayo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor