2

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.

0

5 Answers 5

11

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'.

0
4

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

1
  • Yes, it was line endings, I used vi, I'm sure dos2unix would work too.
    – Alex
    May 22, 2009 at 15:11
1

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

2
  • You are right, I checked, it also does not work when not importing.
    – Alex
    May 22, 2009 at 14:11
  • It also won't start with no space.
    – Alex
    May 22, 2009 at 14:21
0

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
$
0

Use the below one liners to remove the new line characters.

perl -e 's/\r//g' -w -p -i test.py

or

sed -e "s/^M//" test.py > test_new.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .