How to make an executable .py available everywhere on OS X?

Solution 1:

You need to add /foo/bar to your $PATH environment variable. Navigate to your home directory by typing cd at the prompt, then type nano .profile. Inside this file, add the following line:

export PATH=$PATH:/foo/bar

then save and quit. Exit Terminal.app (or whichever term program you're using) and restart it. /foo/bar should now be in your search path, to make sure type echo $PATH and see if it's at the end. You should now be able to run myfile.py from anywhere.

Solution 2:

At your terminal type:

which python

make sure that this path in the shebang at in the 1st line of your .py file. On my system it is

#!/usr/local/bin/python

then 1st try:

export PATH=$PATH:/path/to/where/your/script/is

in any terminal

check that the PATH actually stuck by checking:

$PATH

should have path to your script in there now.

you should now be able to launch you script form anywhere.

the PATH will reset once you close terminal though so you should get pay do get it added permanently to your PATH.

good solution is to have the following bit of code in you .bash_profile:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

see writeup e.g. http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html this will ensure that .bashrc is loaded every time the terminal is launched as well as when you launch a subshell. Put the export

export PATH=$PATH:/path/to/where/your/script/is

line somewhere in your .bashrc file. quit and restart terminal. That should sort everything out