I have a python script 'monty.py' with

#!/usr/bin/env python

in the first line. When I run monty.py in the terminal, I get

monty.py: command not found

I am in the correct directory, and I also have chmod'ed it to be executable. Why won't this run? I am running Ubuntu 10.10 in vmware player.

Edit: Also, when I run /usr/bin/env python from the command line, the python interpreter starts up. So it is in the right place.

Edit edit: I figured it out. Apparently I don't know how to use chmod.

link|improve this question
feedback

3 Answers

up vote 1 down vote accepted
python /path/to/monty/monty.py

Or

cd /path/to/monty
./monty.py

To execute it the second way (./) the monty.py file must be marked as executable:

chmod +x /path/to/monty/monty.py
link|improve this answer
It runs when I use python monty.py. I just don't want to type "python" every time. Also ./monty.py doesn't work, see my comment above. – Java man Apr 13 '11 at 17:35
See my edit about chmod +x – Amazed Apr 13 '11 at 17:37
Did that before I asked the question. – Java man Apr 13 '11 at 17:39
Try issuing this command: which python and verify that the output matches the path you specified in monty.py (/usr/bin/env python) – Amazed Apr 13 '11 at 17:46
which python gives me /usr/bin/python. I tried changing the first line in the file, still no dice. – Java man Apr 13 '11 at 17:48
feedback

Did you create monty.py in a windows text editor? If so, try dos2unix monty.py

If the file has <cr><nl> endings, then the system will see #!/usr/bin/env python\r and "python\r" can't be found.

link|improve this answer
Written in gedit on ubuntu. – Java man Apr 13 '11 at 18:24
@Java, just to check, what does head -1 monty.py | od -c show you as the line ending? – glenn jackman Apr 13 '11 at 18:41
#!/usr/bin/python\n – Java man Apr 13 '11 at 19:18
@Java, please update the question with the current state. I understand that ./monty.py gives "permission denied" and monty.py gives "command not found". Is that right? You might want to show us an ls -l listing of the file. – glenn jackman Apr 13 '11 at 19:33
feedback

Unless . (the current directory) is in your path, you won't search in the current directory for the file you're running EVEN THOUGH it's in your current directory. Instead, try running ./monty.py

link|improve this answer
Stupid comment formatting. ./monty.py bash: ./monty.py: Permission denied sudo ./monty.py sudo: /.monty.py: command not found – Java man Apr 13 '11 at 17:30
feedback

Your Answer

 
or
required, but never shown

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