Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shebang executable not found because of UTF-8 BOM (Byte Order Mark)

Tags:

python

bash

For some reason the shebang in one of my scripts does not work:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
print "Hello World"

When I execute this file, I get an error

 % ./test.py
./test.py: 1: #!/usr/bin/env: not found

There is no problem with the content of my /usr/bin/ directory: both env and python are there, with correct execution rights.

like image 712
Sébastien Avatar asked Sep 28 '13 09:09

Sébastien


People also ask

What does UTF-8 with BOM mean?

The UTF-8 file signature (commonly also called a "BOM") identifies the encoding format rather than the byte order of the document. UTF-8 is a linear sequence of bytes and not sequence of 2-byte or 4-byte units where the byte order is important.

How do I know if my BOM is UTF-8?

To check if BOM character exists, open the file in Notepad++ and look at the bottom right corner. If it says UTF-8-BOM then the file contains BOM character.

How do you use shebang in Python 3?

A shebang line #!/usr/bin/python3 Its purpose is to define the location of the interpreter. By adding the line #!/usr/bin/python3 on the top of the script, we can run the file.py on a Unix system and automatically will understand that this is a python script. Alternative, you could run the script as python3 file.py .


1 Answers

This is due to how Unix and Linux handle the shebang. #! must be the first two bytes in the file. If you have a BOM then this isn't true anymore hence the error.

Note that putting a BOM is completely useless from the point of view of the python interpreter, since the # -*- coding: utf-8 -*- already tells python the encoding.

AFAIK BOM is usually not used with utf-8. It is used for UTF-16 et similia in order to specify the byte-order. If the editor assumes the wrong encoding you should be able to explicitly open the file with the correct encoding.

like image 80
Bakuriu Avatar answered Sep 22 '22 12:09

Bakuriu