5

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.

2

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.

  • 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". – ilovebigmacs 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? – ilovebigmacs 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. – ilovebigmacs Jun 20 '13 at 17:50
1

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

0

Which python are you targeting?

Did you install it with brew? It uses a different path.

which python3 or which python

Choose the one you want

Copy that output

Paste it at the top of your python file

add a #! in front of that path so it looks something like

#!/usr/local/bin/python3

Make sure to change the file permissions

chmod +x filename

Put that file in a folder that is in your path

Not sure if your folder is in your path?

echo $path

How to add that folder to your path?

Find your path first

echo $HOME

If you are using bash or zsh you might have something like this

In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file

export PYTHON_UTILS="$HOME/code/python/utils"

export PATH="$PYTHON_UTILS:$PATH"

Consider removing the .py from your file bc it is not needed in this case

Close and open your terminal, which is sourcing your file by its path

And now you should be able to treat your python file similar to a bash command

You don't need to use python3 filename.py to run the file, you can just use filename

From anywhere on your filesystem!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .