Forum Archive

Bug report: Coding comment for Unicode literals on the SECOND line of a script

ccc
#!/usr/bin/env python
# -*- coding: utf-8 -*-
friends_with_unicode = ('Günter', 'Clauß', 'Ürsala')
print('\n'.join(sorted(friends_with_unicode)))

'''
 If   the shabang ('#!') line is the first line of the script
 And  the coding: utf-8 line is the second line of the script
 Then it is recognized as a Python 2 script with unicode
      literals on non-Pythonista platforms (e.g. Mac OS X)
 But  Pythonista will raise an Encoding Warning and not allow
      the script to run without an "Auto-Fix" option which
      will duplicate line 2 as the new first line of the
      script which breaks portability to other platforms.
'''

http://docs.python.org/2/howto/unicode.html#unicode-literals-in-python-source-code says "Python supports writing Unicode literals in any encoding, but you have to declare the encoding being used. This is done by including a special comment as either the first or second line of the source file"

Pythonista is currently only looking for this special comment on the first but not on the second line of the script.

bdesham

This issue still seems to be present in Pythonista 1.4. Any chance that it will be fixed soon? It would be great to be able to have a command-line-runnable script that would also not make Pythonista complain about the encoding.

MartinPacker

Is there a wider question of good techniques for writing scripts that could run satisfactorily from Bash, from Pythonista, and from Editorial?

omz

This issue still seems to be present in Pythonista 1.4. Any chance that it will be fixed soon?

Yes, this will be fixed in 1.5.

bdesham

@omz Terrific, thanks!

ccc

@MartinPacker, Once we can move to Pythonista 1.5 then starting your scripts with the following two lines will allow them to run smoothly in Linux and Mac OS X environments (bash, etc.)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
MartinPacker

@ccc Thanks. I expect there are other gotchas for portability like how to access raw data. But maybe that's only an Editorial issue and Pythonista is in line with other Python environments.