I'm trying to run Python scripts with a shebang on Ubuntu. When I create a python script

    #! /usr/bin/env python
    import sys

... and run it I get a shell error:

    root@host:/home/user# ./test.py
    : No such file or directory

How can I make it work?

Solution: Remove '\r's from line endings with dos2unix.

link|improve this question

68% accept rate
feedback

5 Answers

up vote 9 down vote accepted

I assume the script is executable? Also, check for carriage returns -- maybe windows got its dirty little hands on it? You can check this with 'cat -vE test.py' and look for '\r'.

link|improve this answer
feedback

You probably have windows line endings on your file. Please try running dos2unix on it.

link|improve this answer
Yes, it was line endings, I used vi, I'm sure dos2unix would work too. – Alex May 22 '09 at 15:11
feedback

Try removing the space between #! and /usr/bin/env, though I don't get why that would work when not importing packages...

link|improve this answer
You are right, I checked, it also does not work when not importing. – Alex May 22 '09 at 14:11
It also won't start with no space. – Alex May 22 '09 at 14:21
feedback

Are you sure that the error is happening for the reason you think? Or are you (later in the script) trying to open a file? Start with the simplest case to get things working...

hello.py:

#!/usr/bin/env python
print "Hello, world"

then...

$ chmod +x hello.py
$ ./hello.py
Hello, world
$
link|improve this answer
feedback

or try this way:

hello.py:

#!/usr/bin/env python
print "Hello, world"

ly@ubuntu: python hello.py

Hello, world
link|improve this answer
1  
What does your answer provide extra to this question? – Lucas Kauffman Apr 10 at 8:51
@LucasKauffman Good question - but maybe a little late ;-) – glglgl Apr 10 at 11:39
Why is my comment too late, the person that added this answer only added it about 23 hours ago... – Lucas Kauffman Apr 10 at 11:46
feedback

Your Answer

 
or
required, but never shown

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