Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am not able to run this pyhton file from terminal. I have added the shebang #!/usr/bin/env python at start of the file.

enter image description here

Am I doing something wrong? How can I run this file from terminal?

EDIT: https://imgur.com/YYBpILr I guess, this might be helpful.

share|improve this question
    
is it executable ? –  nux Apr 23 at 13:15
    
shouldn't the shebang be /usr/bin/python, and not /usr/bin/env python? –  udiboy1209 Apr 23 at 13:19
    
also, can you simply run python aaa.py or already simply python? –  don.joey Apr 23 at 13:20
1  
For the sake of argument, what happens if you explicitly run python aaa.py? –  Oli Apr 23 at 13:48
2  
Look at your last screenshot: you have an invisible char ^M after the python in the shebang line. This is probably a remnant of having edited the file on a non-Unix machine (typically, Windows). Delete that char and it will work --- if you had cut and pasted that error in the question, without screenshots, you have had the answer instantly... –  Rmano Apr 23 at 14:08

2 Answers 2

up vote 3 down vote accepted

The screenshot below show the problem: you have an invisible char ^M after the python in the shebang line. This is probably a remnant of having edited the file on a non-Unix machine (typically, Windows); so the interpreter looked for is python^M which is not found.

If you haven't edited this file on a alien os, maybe it's a problem with the line-ending setting on your editor. Anyway, rewriting the shebang line in a sane editor should fix the thing up.

enter image description here

share|improve this answer
    
I have faced this problem earlier. ^M was visible only in terminal editors like nano and not in graphical editors like gedit or sublime text. Had to edit the file in nano to remove all the ^M characters from the file. It worked fine thereafter. –  Aditya Apr 23 at 14:25
1  
@Aditya: no need to do that manually. Sublime for example lets you see and change line endings with 2 clicks, you just need to set "show_line_endings": true in the preferences. –  LeartS Apr 23 at 14:32
    
@LeartS: Yes. It's set to false by default. But good to know if I encounter it again in future :) –  Aditya Apr 23 at 14:34

Your file has Windows line endings, which unix shells don't like.

Just convert them to Unix file endings using dos2unix aaa.py (you may need to install dos2unix, which is a very light [200kB] but useful software) and you should be fine.


Different line endings representations are a common source of "strange" problems. You can check line endings with file <yourfile> if it says something like:

<yourfile>: ASCII text,with CRLF line terminators

It means it has Windows line endings and you should convert them to unix, using dos2unix, the vim command :set ff=unix or your favourite text editor if it has the feature (many do).

share|improve this answer
    
i.imgur.com/hFkElwK.png I think, I did got rid of the line ending problem. but its still giving me the same output. :No such file or directory. file aaa.py gives output aaa.py, Python script, ASCII text executable. –  Potato_head Apr 23 at 14:58
    
It does not give you the same output: before it was : no such file or directory now it is usr/bin/python: bad interpreter: No such file or directory Did you change the shebang in the meantime, maybe from #!/usr/bin/env python to #!usr/bin/python? If you did, that is now the problem. Go back to the original one. –  LeartS Apr 23 at 15:07
    
Yes, I used #!usr/bin/python instead of #!usr/bin/env python. Reverting it did't help. –  Potato_head Apr 23 at 15:12
1  
They are both wrong, a leading slash is missing. A leading slash indicates an absolute uri, without it is relative to the current directory and you don't have a usr/bin/python (nor env) in the current directory. The correct shebang is: #!/usr/bin/env python. –  LeartS Apr 23 at 15:13
1  
Yes, You are right. I messed it up. Thanks. –  Potato_head Apr 23 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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