I'm trying to make a python script runnable, by adding the following "shebang" first line:

#!/usr/bin/env python

but when I run the file, I get:

$ myscript.py
: No such file or directory

Now, if I just try the env line directly, it works:

]$ /usr/bin/env python
Python 2.6 (r26:66714, May  5 2010, 14:02:39)
etc. etc.

Why is the same line failing inside the file, but succeeding outside of it?

  • 2
    Does this answer your question? #!/bin/bash - no such file or directory – Uncle Billy Mar 12 at 15:39
  • @UncleBilly: Not quite, because that's a different error message. But - it's the same answer. So, up to you whether to mark this a dupe. – einpoklum Mar 12 at 15:44
  • 1
    The benefit of duplicates is that they are multiple signposts, each with their own wording & variation, that point to the same underlying issue. I would consider this a duplicate as it's the same underlying problem. – Jeff Schaller Mar 12 at 15:49

You probably have an invalid character somewhere in your shebang line. Since you're the one who inserted it, it's likely a carriage-return, i.e. your line ends with a newline and a carriage return (or vice-versa) - in the ODS style of line breaking.

Try using dos2unix on your script file to convert all the line breaks to only be 0x10 (UNIX-style).

| improve this answer | |

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