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.
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With