env: python\r: No such file or directory

Niklas R picture Niklas R · Oct 17, 2013 · Viewed 58.4k times · Source

My Python script beak contains the following shebang:

#!/usr/bin/env python

When I run the script $ ./beak, I get

env: python\r: No such file or directory

I previously pulled this script from a repository. What could be the reason for this?

Answer

ccpizza picture ccpizza · Apr 27, 2016

Open the file in vim or vi, and administer the following command:

:set ff=unix

Save and exit:

:wq

Done!

Explanation

ff stands for file format, and can accept the values of unix (\n), dos (\r\n) and mac (\r) (only meant to be used on pre-intel macs, on modern macs use unix).

To read more about the ff command:

:help ff

:wq stands for Write and Quit, a faster equivalent is Shift+zz (i.e. hold down Shift then press z twice).

Both commands must be used in command mode.

Usage on multiple files

It is not necessary to actually open the file in vim. The modification can be made directly from the command line:

 vi +':wq ++ff=unix' file_with_dos_linebreaks.py

To process multiple *.py files (in bash):

for file in *.py ; do
    vi +':w ++ff=unix' +':q' "${file}"
done

😱 offtopic: if by chance you are stuck in vim and need to exit, here are some easy ways.