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

pathpython

I have this directory /foo/bar/. Inside, I have a python file named myfile.py. I made it executable by adding (at the top):

#!/usr/bin/env python

Then, I made it executable by issuing

chmod +x myfile.py

I edited my path by issuing

sudo nano /etc/paths

I added /foo/bar/ to it.

The problem is that although I can run myfile.py by typing ./myfile.py while I am inside /foo/bar/, it doesn't work from any other location. What am I doing wrong?

Thank you.

Best Answer

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.