3

As PEP 263 says, Python 2 (specifically 2.7 in my case) requires a magic comment line to specify a file encoding:

# -*- coding: utf-8 -*-

Without that it is not possible to include non-ASCII characters in the file. A notorious error would be raised:

SyntaxError: Non-ASCII character '[…]' in file […] on line […], but no encoding declared; see http://python.org/dev/peps/pep-0263/

However if the non-ASCII character is in the shebang interpreter path, the magic comment does not work:

#!/Users/naïve/project/python2.7
# -*- coding: utf-8 -*-
print 'Hello.'

This happens for example if you create a virtualenv in a folder with a non-ASCII path: Many scripts with a shebang that breaks the script are created.

The only workaround I could come up with is to symlink the folder into some ASCII-only path. Then it is possible to change the shebang path to the ASCII one. Or in the case of virtualenv to initialize it from the symlinked path.

However this cannot be done if the non-ASCII part of the path is the user’s home path and the user does not have administration access. So I’d like to ask: Is there any other workaround for this situation, hopefully more clean? E.g. some default file encoding setting or environment variable that would Python use if it does’t find a magic comment?

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Glutexo
  • 547
  • 6
  • 13

1 Answers1

0

If you save the file as utf-8-sig instead of utf-8, you don't need an encoding-cookie at all. That is, use a unicode byte-order mark when saving the file.

In python you can use utf-8-sig as an encoding-parameter. In VIM you can use :set bomb to explicitly save the file with a BOM.

user2722968
  • 13,636
  • 2
  • 46
  • 67
  • Interesting. When I save the file with BOM, the system (macOS) completely ignores the shebang. It runs it as a `bash` script instead of the Python specified. – Glutexo May 14 '18 at 14:20