views:

142

answers:

6
+1  Q: 

Python in terminal

Hi. This question concerns running python files in terminal that are not stored in the home directory. I think I have solved the first bit of this puzzle by modifying my path so that it includes the directory where my python programs are stored.

So where as initially echo $PATH would yield the following: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

it now yields: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/paulpatterson/Documents/Python

However despite the fact that the correct folder is now in my path, none of the python files within this folder run. For example there is a file in there called recap.py, when I open terminal and type in: python recap.py I get:

python: can't open file 'recap.py': [Errno 2] No such file or directory

If I simply type in recap.py (i.e. omitting the 'python' bit), I get: -bash: /Users/paulpatterson/Documents/Python/recap.py: Permission denied

Can anyone enlighten me? Ideally I want to set it up so as soon as terminal opens all I need to do is type the file name and not even type python.

I've spent hours trying to sort this out, any help is appreciated.

Paul.

+1  A: 

Make sure the first line in every script is "#!/usr/bin/env python" (no qoutes). Do "chmod +x file.py" for every file. You should then be able to run each script as file.py.

mikerobi
Thanks for the swift response. Have added suggested line to top of file, but when I try the chmod thing I get:chmod: recap.py: No such file or directorySo still can't get script to run
Paul Patterson
If you ran `chmod +x file.py`, then `file.py` needs to be in your current working directory or you need to specify the full path to the file. chmod isn't going to search your entire filesystem for a random file.py and chmod it.
jamessan
@Paul Patterson, you need change to the directory containing file.py or give the full path to chmod. "cd /Users/paulpatterson/Documents/Python/" first, or do "chmod +x /Users/paulpatterson/Documents/Python/file.py"
mikerobi
Okay, i've specified the full path using: chmod u+x /Users/paulpatterson/Documents/Python/recap.py.File is now found, but doesn't execute properly; terminal displays the following: /Users/paulpatterson/Documents/Python/recap.py: line 1: #!/usr/bin/env: No such file or directory/Users/paulpatterson/Documents/Python/recap.py: line 3: syntax error near unexpected token `('/Users/paulpatterson/Documents/Python/recap.py: line 3: `start_num = int(raw_input("What number do you wish to start counting at?"))'So suggests lots of errors, but file runs perfectly if I run it from paulpatterson
Paul Patterson
@Paul Patterson, try replacing "/user/bin/env" with "/bin/env"
mikerobi
Okay, done this but still get same error output as above.
Paul Patterson
+6  A: 

Including the directory where a command lives in your $PATH means you can run commands in that directory from anywhere. But in your first example, you are running the command "python" with recap.py as an argument. So your shell does not search your $PATH to find where recap.py lives. To make recap.py runnable as a command by itself, see this:

http://en.wikipedia.org/wiki/Shebang_(Unix)

In short, you need to include #!/usr/bin/env python as the first line, and chmod the file to be executable (chmod u+x recap.py).

dreeves
+1  A: 

You may have tried this before, but I ran into similar problems at one point and this is the process that works for me. In the directory where recap.py is stored:

chmod +x recap.py
./recap.py

The ./ being the key part, as it works as the full path to the directory. This also assumes that you have

#!/usr/bin/env python

or something similar as the first line of your program. The first two characters first line is called a shebang and indicates that the file is a script to be executed by the interpreter specified by the path following it. So, as others have suggested, your python interpreter may be located somewhere other than /usr/bin (possibly in /bin), so you'll need to find this, and include this line at the top of any python script you wish to execute from the terminal.

If you don't know where python is located, you can use:

which python

at the terminal, which should print the path to your python install. You can then use that path after your shebang.

Wilduck
Hi, pretty sure python is in /usr/bin. Macbook is less than two weeks old, and when I type: ls -l /usr/bin into terminal I get a whole load of files, including python ones. I've tried both suggestions but still getting the same error message:/Users/paulpatterson/Documents/Python/recap.py: line 1: #!/usr/bin/env: No such file or directory/Users/paulpatterson/Documents/Python/recap.py: line 3: syntax error near unexpected token `('/Users/paulpatterson/Documents/Python/recap.py: line 3: `start_num = int(raw_input("What number do you wish to start counting at?"))'
Paul Patterson
./recap.py simply results in a file not found message
Paul Patterson
./recap.py should not result in a file not found message if you have a file named "recap.py" in the directory which you're working. (type pwd to see your working directory, and ls to see its contents) To see if /usr/bin is the correct directory, there should be a file named 'env' and one named 'python'. I think on a mac python might be installed in /usr/local/bin/python
Wilduck
to find out for sure where python is installed type 'which python' into your terminal and use that after your shebang.
Wilduck
Checked /usr/bin and there is indeed a file named 'python' and one named 'env'. 'Which python' yields '/usr/bin/python'. So that seems to be as it should be. Unfortunately still no dice. I'm not sure what you mean by the directory in which I'm working. If i put the file in the directory 'paulpatterson' it works fine. What I want to do is put the file (and all subsequent python files) in a directory further downstream i.e. paulpatterson/Documents/Python.
Paul Patterson
So, mini lesson: When you're working at the terminal, at any given moment, you have a "working directory" you can find out what this is by typing pwd (for "print working directory"). The reason this works when the file is in paulpatterson is that it is most likely the directory you're working in (as it's probably your default working directory). If you want these steps to work with the file in a directory downstream, you're going to have to move to that directory using the cd command.
Wilduck
So, 'pwd' shows you're in '/Users/paulpatterson', then you're going to need to use the command 'cd Documents/Python'. Then, when you use 'pwd' it should show '/Users/paulpatterson/Documents/Python'. If you then type 'ls' it should show you the files in that directory. If recap.py is one of them, './recap.py' will run it.
Wilduck
Seriously though, spend an hour on that website Daenyth suggested.
Wilduck
+1  A: 

Type in:

which python 

into terminal and that should get you the path to python. Put that on top of your script as others have suggested:

#! /path/to/python

Also make sure it is executable (the whole chmod stuff). You can check this by typing:

ls -l

The file should then have something like -rwx-r--r-- next to it. The x means it is executable.

Paul
You can avoid the `which` by having `#!/usr/bin/env python`
Daenyth
Thanks. Done all this but still not working. 'which python' gives me /usr/bin/python, so the shebang I've put at the top is '#!/usr/bin/python', but the error message is the same. Using your ls -l suggestion I get-rwxr-xr-x@ 1 paulpatterson staff 327 1 Jul 18:37 recap.pySo that seems to be okay.
Paul Patterson
A: 

Try typing:

which env

to find out what the path of env is on your system. Change the shebang path to match the full path of the env command found by using the which command. If that fails. It means you either don't have env installed or env is installed in a system path. Try specifying the full path to the python executable instead of using env by typing:

which python

And using the full path return in place of the /path/to/env python shebang. The first line of your recap.py should look something like this:

#!/path/to/python
freegnu
+1  A: 

The other responders have done a good job with your initial question, but I think you would be very well served to actually learn how to use the command line, as your replies have shown that you're not familiar with it yet. I always recommend this. It's a fantastic guide that will get you comfortable using it.

Daenyth
Thanks. You're right, I'm not familiar with it yet. Ironically however my initial question was prompted following my attempts to start learning the command line. I bought the following ebook and was attempting a couple of the things it teaches (Take Control of the Mac Command Line with Terminal). I'll give your suggestion a try also.
Paul Patterson
It assumes linux, so there will be some differences, but OSX is still a unix, so it's close enough to get you started, and many of the tools are the same (although it uses a BSD userland rather than GNU). You can also just play around in a vm running linux or on a livecd, or whatever other method
Daenyth