Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
It is at the end of my $PATH (it already was but I tried adding what you proposed to .profile as well). When I type "myfile.py" anywhere, it tells me "command not found". –  Nico S. Jun 18 '13 at 14:10
    
First, make sure everything is spelled correctly - /usr/loacl/bin has gotten me before :). I assume you're using bash as your shell, right? Type source ~/.profile (to make sure it's being read), then type hash to refresh the shell's memory of the search path. –  MattDMo Jun 18 '13 at 18:44
    
When I type source ~/.profile, all I see is a blank line. So I guess it is not being read as when editing it, I see the export PATH=$PATH:/foo/bar line in it. What should I do? –  Nico S. Jun 20 '13 at 14:06
    
You won't see anything when typing source ~/.profile. What the source command coes is read the file and execute what's contained within it. After you run the source command and run hash, are you able to run myfile.py? –  MattDMo Jun 20 '13 at 14:08
    
Yes! It works finally. Thank you! I'll accept your answer as the correct one even though the solution is in comments. –  Nico S. Jun 20 '13 at 17:50
show 1 more comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.