Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 321
Niklas R Avatar asked Oct 19 '22 00:10

Niklas R


People also ask

Why does Python Say No such file or directory?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

Why is there no such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.

What does #!/ Usr bin env python do?

1 Answer. If you have installed many versions of Python, then #!/usr/bin/env ensures that the interpreter will use the first installed version on your environment's $PATH. If you are using Unix, an executable file that is meant to be interpreted can indicate what interpreter to use by having a #!

What does usr bin env mean?

As we mentioned earlier,#!/usr/bin/env bash is also a shebang line used in script files to execute commands with the Bash shell. It uses the env command to display the environment variables present in the system and then execute commands with the defined interpreter.


1 Answers

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.

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 and has a shebang. 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 even with the execution bit set because your shell (sh, bash, zsh, etc) can't handle the BOM mark. (It's usually windows editors such as Notepad which create files with a BOM mark.)

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

:set nobomb
like image 101
ccpizza Avatar answered Oct 23 '22 10:10

ccpizza