1

I have a problem..whenever I am writing any Python script say like this

#!/usr/local/bin/python
print "hello"

Then using

chmod +x a.py

And then write ./a.py then it is not printing anything in the terminal

Moreover whenever I write any comment below the shabang line, it gives me an error saying #: bad interpreter : No such file or directory

but when I run the script like this python a.py it works as usual..

Can someone tell me what's wrong and how to fix this..

13
  • Try running which python Commented Aug 21, 2015 at 15:15
  • I always use "#!/usr/bin/env python", which automatically locates python in your environment. Commented Aug 21, 2015 at 15:18
  • I have the correct Python path...one more thing..when I open this file in vim, it tells me noeol but while opening other files it is not telling me and hence other files are working properly Commented Aug 21, 2015 at 15:18
  • 1
    I'm fairly certain if your path was correct you wouldn't be receiving this error. Commented Aug 21, 2015 at 15:21
  • 1
    @martineau it's supposed to be the path to the executable program, which will be run with the script file as its standard input. As you wrote it, it would try to run the directory /usr/local/bin with "python" as an argument Commented Aug 21, 2015 at 15:46

2 Answers 2

6

This is almost certainly because your line ending is a carriage-return/line feed combination - which Windows-style editors will create. Unix regards the LF as the end of the line, so it's looking for an executable called "python\r". When you run it with an explicit call to the interpreter the shebang line is just treated as a comment.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried the \r option too..I think that there is some problem with the file..because this error is not present for other Python files..needless to say that this file shows noeol ( no end of line) below while others are not showing that
What do you mean you "tried the \r option too"? Just make sure there are no carriage returns in the file
@holdenweb oups, sorry :-(
0

Under linux text replacement fix:

sed -i 's/^ M//g' filename 

(note that ^ M is written in linux, press ^ M is carriage return and line feed, the input method is to hold down CTRL + v, release v, press m)

This solve bad endline for shell in first line if error message looks like this:

/usr/bin/python3 ^M: bad interpreter: No such file or directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.