pythonmacososx-mountain-lionshebangenv

env: python\r: No such file or directory


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?


Solution

  • The script contains CR characters. The shell interprets these CR characters as arguments.

    Solution: Remove the CR characters from the script using the following script.

    with open('beak', 'rb+') as f:
        content = f.read()
        f.seek(0)
        f.write(content.replace(b'\r', b''))
        f.truncate()