Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...