-1

On Linux (Ubuntu 11.04), for some reason, a Python script (specifically the Django's manage.py, but I don't think that matters) recently started acting strangely:

When run as

python manage.py

it runs just fine; however, with

./manage.py

it shows the following error:

: No such file or directory

The file's permissions are 766. Any ideas?

Berislav Lopac
  • 16,656
  • 6
  • 71
  • 80

5 Answers5

6

Your file has carriage returns in it. Did you write it in a Windows text editor?

Try running dos2unix manage.py manage.py

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
3

glenn jackman's answer is correct, but I don't have enough "reputation" to upvote him, so I'll post this here. Your script is in Windows format, in which every line ends with a carriage return and linefeed, instead of just a linefeed. Many programs, including python, can handle either format with no problem. But when you run the script, the shell believes that the carriage return is part of the command name. Instead of running "/usr/bin/env python", your shell is trying to run "/usr/bin/env python^M" (where ^M is a linefeed). You can tell this is the case because of the error message it gives you. Just before "No such file or directory", it prints the name of the program it tried to execute. It printed the linefeed too, which moved the cursor back to the leftmost position in the line, which erased everything before the colon.

If you don't have dos2unix installed, you can remove the linefeeds with

tr -d '\r' < manage.py > manage2.py; mv manage2.py manage.py

You can't read from and write to the same file at the same time, which is why you have to use a temporary file to hold the output of tr.

Jonathan
  • 1,163
  • 7
  • 12
  • 1
    Ugh. You'd think the shell would be more robust than that. – Tom Zych Oct 06 '11 at 21:36
  • I'm not sure what you're expecting the shell to do, @Tom Zych. If you tell it to run a program called /usr/bin/python^M, and that file doesn't exist, what *should* it do? – jlp Oct 08 '11 at 06:59
  • 1
    It should recognize that `^M` is part of the line terminator and ignore it. This is not rocket science. – Tom Zych Oct 08 '11 at 11:09
2

I suspect that the shebang at the top of the file is incorrect. The file should start with either:

#!/usr/bin/python

(where the python path is the output of which python)

or

#!/usr/bin/env python
Danny Staple
  • 7,101
  • 4
  • 43
  • 56
1

If the permissions are 766 and you're not the owner, you don't have permission to execute it. 6 means you can read and write but not execute. It's unusual for a system file like that to be world-writeable; normally it would be 755. If you have rootly powers, use chmod 755 manage.py to fix it.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
  • If he didn't have execute permission, it would say: `permission denied`, not `file not found` – tMC Oct 06 '11 at 19:46
  • Right, good point. Still, it shouldn't be world-writeable, it's a security hole. Someone could hide a `rm -rf ~` in it. – Tom Zych Oct 06 '11 at 21:35
0

When you run a script directly, the script is started with the interpreter specified in the first line:

#!COMMAND

Where COMMAND is /bin/bash for shell scripts. For python, it's best to use

#!/usr/bin/env python

So that the version of python from the environment is selected.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137