Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run my python script from the terminal in Mac OS X without having to type the full path?

I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.

I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.

Here's the contents of my .profile file in my home directory:

~ toby$ vim .profile 
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH

Here's the output of echo $PATH, where all seems well:

~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

Here's the script I'm trying to run:

~ toby$ ls /Users/tobylieven/Documents/my_scripts 
-rwxrwxrwx@ 1 tobylieven  staff  276 17 Jan 21:17 alarm.py

Here's the command I'm trying to use to run the script and the fail message I'm getting instead:

~ toby$ python alarm.py 
python: can't open file 'alarm.py': [Errno 2] No such file or directory

If anyone has an idea what I might be doing wrong, that'd be great. Thanks a lot.

like image 224
tobuslieven Avatar asked Jan 17 '11 21:01

tobuslieven


2 Answers

PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:

#!/usr/bin/env python

and run

sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py

Then, you can type just alarm.py to execute your program.

like image 136
phihag Avatar answered Oct 19 '22 03:10

phihag


change alarm.py to include:

#!/bin/python

as the very first line in the file.

(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python in the terminal.)

You can then just run alarm.py instead of python alarm.py.

e.g.:

~ toby$ alarm.py  

And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.

like image 34
Matt Wonlaw Avatar answered Oct 19 '22 04:10

Matt Wonlaw