[Pythonmac-SIG] Run Python module directly from Terminal?

Lee Cullens lee_cullens at mac.com
Tue May 24 06:36:37 CEST 2005


It's a little less trouble Les.

I added the shbang to my scripts (with Tiger and/or Bob's new right  
setup the env is dropped)

      #!/usr/local/bin/python2.4

And changed my shell .profile to

      export PS1="\u \w \! \$"
      export PATH=~/PythonProjects/MyUtilities:$PATH         #added  
line without this comment

      ##
      # DELUXE-USR-LOCAL-BIN-INSERT
      # (do not remove this comment)
      ##
      echo $PATH | grep -q -s "/usr/local/bin"
      if [ $? -eq 1 ] ; then
          PATH=$PATH:/usr/local/bin
          export PATH
      fi

Then in the Terminal did

      Chinook ~ 99 $cd ~/PythonProjects/MyUtilities
      Chinook ~/PythonProjects/MyUtilities 100 $chmod a+x ConvertFile.py
      Chinook ~/PythonProjects/MyUtilities 101 $ConvertFile.py AtoB.txt

which works fine with AtoB.txt in my cd, but otherwise I would need  
something like

      Chinook ~/PythonProjects/MyUtilities 102 $cd ~
      Chinook ~ 103 $ConvertFile.py ~/TestFiles/AtoB.txt

So I still need to set the script file permissions (at least if I  
save the script to a new name as a variation) and provide an adequate  
path to the arg file (no magic for that I know of other than a  
shorthand alias :~)

I guess if I would stop looking for magic bullets I'd have time to  
write a python shell script with which to select a utility script and  
any arguments (and provide batch processing).  I was putting that off  
until I start playing around with GUIs (after getting a handle on  
ObjC as Bob suggested).  I'm only just two months into this new  
playground (Mac/Unix(Darwin)/ObjC/Python) and trying to stay on the  
most productive learning curve :~)

At least I'm off on the right foot with my utility scripts written as  
functions and including the if __name__ == '__main__':  block so they  
can be run either way.

Thanks for your help,
Lee C


On May 23, 2005, at 10:22 PM, Les Hill wrote:

> On 5/23/05, Lee Cullens <lee_cullens at mac.com> wrote:
>
>> So what's my problem?  Well I know I could reduce the typing with  
>> some
>> aliasing in my .profile file,  but I'm wondering if there is not a
>> simpler/direct way of accomplishing a basic task such as this?
>>
>
> Not sure if this is what you are asking but doing the following as the
> very first line of the script:
>
> #!/usr/bin/env python
>
> And then adding the directory where the script is located to your
> path, for example in your .profile:
>
> export PATH=~/Python/scripts:$PATH
>
> Finally, make sure the script has the appropriate permissions
> (execute) by doing the following in a shell where you have moved to
> the scripts directory:
>
> chmod a+x SCRIPTNAME
>
> This (admittedly second nature to Unix geeks) straightforward setup
> should allow you to do the following at the shell command line:
>
> wonderland: % your_script_here.py file_to_convert
>
> will invoke your script with your_script_here.py as sys.argv[0] and
> file_to_convert as sys.argv[1].
>
> -- 
> Les Hill
> leshill at gmail.com
>



More information about the Pythonmac-SIG mailing list