2

Take the following code:

#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

thing = u'\N{DEGREE SIGN}'
print thing

This will run in OS X terminal without error, but when run through the Textwrangler Python interpreter, it throws an error. However, when I flip the first two lines, it runs without error in both places:

# -*- coding: utf-8 -*-
#! /usr/bin/env python2.5

thing = u'\N{DEGREE SIGN}'
print thing

I was under the impression that the shebang needed to be in the first two bytes of the file, so I presume that it's being ignored in the second example. I was further under the impression that convention dictated that this is the proper way to kick off the script:

#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

But that doesn't seem to be the case in all instances.

Is there something with Textwrangler here that I'm missing?

[EDIT] My use case (Python 2.5) is at minimum a large part of what is causing the problem.

[UPDATE] The following runs error-free in Terminal and TextWrangler:

#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

degreeSign = u'\u00B0'
degreeSign_encoded = degreeSign.encode('utf-8')
print degreeSign_encoded
DaveL17
  • 1,673
  • 7
  • 24
  • 38
  • The examples in [the PEP](https://www.python.org/dev/peps/pep-0263/) agree - the encoding can be in the first or second line, and the shebang must be first. – jonrsharpe Feb 17 '15 at 16:03
  • Thanks @jonrsharpe. I had read the PEP and had been using that standard without issue until I ran into this particular problem with TextWrangler. I wonder if TextWrangler is automatically inserting a shebang when running the script which would (effectively) force the UTF coding line to line 3... – DaveL17 Feb 17 '15 at 16:14
  • Just to follow up a bit, I learned from TextWrangler support that when the Shebang is ignored--as expected when it's on line 2--TextWrangler uses Python 2.7.5 (in my case) instead of 2.5. Editing the OP to make it more specific. – DaveL17 Feb 18 '15 at 18:59

0 Answers0