4

Lubuntu Raring Ringtail. I want to be able to execute my scripts with ./

I start my python script with #!/usr/bin/python and it says

bash : ./myscript.py: /usr/bin/python^M: bad interpreter: No such file or directory

If I use #!/usr/bin/env python it gives a similar error:

: No such file or directory

What am I doing wrong?

I absolutely have python installed, and can run the scripts as normal with python myscript.py

  • 1
    Have you taken a look at this question: http://stackoverflow.com/questions/9975011/pycharm-usr-bin-pythonm-bad-interpreter ? – Isaiah Aug 16 '13 at 04:27

2 Answers2

10

From the ^M you can see that the file myscript.py is using windows/dos-style line breaks (Windows uses CR LF (carriage return + line feed) at the end of a line. Unix only uses LF - so what you see as ^M is the CR. So what you are not using /usr/bin/python but /usr/bin/python<CR> that does not exist.

You can remove the ^M using dos2unix (do a sudo apt-get install dos2unix to install and then use dos2unix myscript.py).

Thomas
  • 1,656
3

Do this, then try your Python script:

$ tr -d '\r' < test.py > newtest.py

This removes the carriage returns created from Windows

To further reading, Remove Windows carriage returns with tr

Daniel
  • 163