[Tutor] How can I make my script work without calling Python?

D-Man dsh8290@rit.edu
Thu, 29 Mar 2001 10:22:14 -0500


On Thu, Mar 29, 2001 at 12:30:07PM +0100, Karim Yaici wrote:
| 
|    Hello there,
|    
|    I have quick question. let's say I have a script (foo.py) which takes
|    some parameters (x,y,z). the usual way to run is to type:
|    
|    >python foo.py x y z
|    
|    Is there anyway I can make it work without calling Python i.e
|    
|    >foo.py x y z
|    
|    
|    I know I can make it .EXE file (you'll understand that I am running
|    windoze:-) ) but I am sure I have seen people doing it with a raw
|    python script.

On Windows your choices are (1) to make an exe, (2) make the program a
wrapper (ie .bat file) that runs the interpreter on the real script,
or (3) not use the command line.  

Sample of choice number 2 :

---- foo.py ----

# this is my program 
...


---- foo.bat ----
rem AFAIK batch files don't have the equivalence of $* in bash
python foo.py %1 %2 %3 %4 %5 %6 %7 %8 %9



For choice number 3 you need to set the default action.  Open
_windows_ (not internet) explorer, click on view->options then click
the "File Types" tab.  Find .py files and specify (through the
dialogs, ask if you want more details) that it should be run by the
interpreter.  Then when you double click on it in explorer it will run
in the interpreter.  No effect on the command line though.  Of course,
you can't pass commandline arguments using this method (ie sys.argv).


If you use a *nix system (or fake it using cygwin on windows) you can
put

#!/usr/bin/env python

as the very first line of the script, then the shell will know to run
it using python.  (Just one example of why *nix is superior to 'doze
;-))


HTH,
-D