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?