env: python\r: No such file or directory
Asked Answered
P

8

67

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?

Prima answered 17/10, 2013 at 11:46 Comment(0)
K
48

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()
Kellner answered 17/10, 2013 at 11:55 Comment(7)
@NiklasR, See the screencast I just recorded. The error message is slightly different because I recorded it in Linux machine.Kellner
Thanks a lot for your answer and your screencast. I understand the problem now :)Prima
@downvoter, Any reason for downvoting this? How can I improve the answer?Kellner
And by "CR" we mean "carriage return" (ASCII 13).Morphophoneme
I just resolved that issue. Your current file is "CR" type or any thing else, so you have to open that file by notepad++ or any editor and convert that to "LF". Notepad++: Edit menu -> EOL Conversion -> Unix (LF), and then save that Pic: i.imgur.com/paAPYsK.png Hope it useful for you guys.Woodhead
any idea how these characters got there in the first place?Hardaway
@embulldogs99, Windows use CR, LF for line ending. en.wikipedia.org/wiki/Newline#HistoryKellner
G
103

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.

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

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

While it looks innocent, the bash syntax above will break on file names containing spaces, quotes, dashes, etc, a more reliable alternative that will also process subfolders would be:

find . -iname '*.py' -exec vi +':w ++ff=unix' +':q' {} \;

Removing the BOM mark

Sometimes even after setting unix line endings you might still get an error running the file, especially if the file is executable, has a shebang, and you are running it without prefixing it with python. The script might have a BOM marker (such as 0xEFBBBF or other) which makes the shebang invalid and causes the shell to complain. In these cases python myscript.py will work fine (since python can handle the BOM) but ./myscript.py will fail when the execution bit is set because your shell (sh, bash, zsh, etc) can't handle the BOM mark. (It's usually windows editors such as Notepad that create files with a BOM mark.)

The BOM can be removed by opening the file in vim and administering the following command:

:set nobomb
Gallery answered 27/4, 2016 at 9:33 Comment(1)
Used this on both pdf2txt.py and dumppdf.py which i installed by pip install pdfminer.six on my Ubuntu machine. Now errors are gone and they work as they shouldInternuncial
K
48

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()
Kellner answered 17/10, 2013 at 11:55 Comment(7)
@NiklasR, See the screencast I just recorded. The error message is slightly different because I recorded it in Linux machine.Kellner
Thanks a lot for your answer and your screencast. I understand the problem now :)Prima
@downvoter, Any reason for downvoting this? How can I improve the answer?Kellner
And by "CR" we mean "carriage return" (ASCII 13).Morphophoneme
I just resolved that issue. Your current file is "CR" type or any thing else, so you have to open that file by notepad++ or any editor and convert that to "LF". Notepad++: Edit menu -> EOL Conversion -> Unix (LF), and then save that Pic: i.imgur.com/paAPYsK.png Hope it useful for you guys.Woodhead
any idea how these characters got there in the first place?Hardaway
@embulldogs99, Windows use CR, LF for line ending. en.wikipedia.org/wiki/Newline#HistoryKellner
G
26

You can convert the line ending into *nix-friendly ones with

dos2unix beak
Glyconeogenesis answered 19/3, 2014 at 4:36 Comment(1)
This worked for me. I got this error when trying to run ~/leo-5.0/launchLeo.py to open the Leo editor. To get it to work, I had to install dos2unix first using Homebrew like so: brew install dos2unixVelarize
A
14

If you use PyCharm you can easily solve it by set the line separator to LF. See my screenshot. As you can see you can set it at the bottom right corner

Artair answered 30/9, 2018 at 12:43 Comment(1)
didnt work for meI
V
4

I cured this error by running python3, ie python3 \path\filename.py

Vivid answered 12/6, 2020 at 7:53 Comment(1)
I suspect that the python script was developed on Windows which has different line endings from Unix/Linux: Windows uses \r\n; Unix and Linux use \n. If you edit the file, try removing the \r at the end of the first line and then “./beak” should work.Olio
P
2

The answer of falsetru did absolutely solve my problem. I wrote a small helper that allows me to normalize line-endings of multiple files. As I am not very familar with the line-endings stuff on multiple platforms, etc. the terminology used in the program might not be 100% correct.

#!/usr/bin/env python
# Copyright (c) 2013  Niklas Rosenstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import sys
import glob
import argparse

def process_file(name, lend):
    with open(name, 'rb') as fl:
        data = fl.read()

    data = data.replace('\r\n', '\n').replace('\r', '\n')
    data = data.replace('\n', lend)
    with open(name, 'wb') as fl:
        fl.write(data)

def main():
    parser = argparse.ArgumentParser(description='Convert line-endings of one '
            'or more files.')
    parser.add_argument('-r', '--recursive', action='store_true',
            help='Process all files in a given directory recursively.')
    parser.add_argument('-d', '--dest', default='unix',
            choices=('unix', 'windows'), help='The destination line-ending '
            'type. Default is unix.')
    parser.add_argument('-e', '--is-expr', action='store_true',
            help='Arguments passed for the FILE parameter are treated as '
            'glob expressions.')
    parser.add_argument('-x', '--dont-issue', help='Do not issue missing files.',
            action='store_true')
    parser.add_argument('files', metavar='FILE', nargs='*',
            help='The files or directories to process.')
    args = parser.parse_args()

    # Determine the new line-ending.
    if args.dest == 'unix':
        lend = '\n'
    else:
        lend = '\r\n'

    # Process the files/direcories.
    if not args.is_expr:
        for name in args.files:
            if os.path.isfile(name):
                process_file(name, lend)
            elif os.path.isdir(name) and args.recursive:
                for dirpath, dirnames, files in os.walk(name):
                    for fn in files:
                        fn = os.path.join(dirpath, fn)
                        process_file(fn, fn)
            elif not args.dont_issue:
                parser.error("File '%s' does not exist." % name)
    else:
        if not args.recursive:
            for name in args.files:
                for fn in glob.iglob(name):
                    process_file(fn, lend)
        else:
            for name in args.files:
                for dirpath, dirnames, files in os.walk('.'):
                    for fn in glob.iglob(os.path.join(dirpath, name)):
                        process_file(fn, lend)

if __name__ == "__main__":
    main()
Prima answered 18/10, 2013 at 12:39 Comment(0)
B
1

If you are using vscode or pycharm, set the end of line sequence to LF. This solves it.

Benuecongo answered 5/10, 2023 at 6:50 Comment(0)
C
0

I tried the first solution using vi / set ff=unix, but it didn't work. I found a very very simple way of solving it:

I opened the python file in the text editor Mousepad (on a R Pi), selected Document/Line Ending, and changed it from "DOS / Windows (CR LF)" to "Unix (LF)", then saved it. It's possible I also re-typed the shebang at the top, can't remember. It works now.

Conner answered 25/6, 2023 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.