[Tutor] Puzzled again
Steven D'Aprano
steve at pearwood.info
Wed Aug 3 11:20:45 CEST 2011
Richard D. Moores wrote:
> But here's a try using the regular command line:
>
> C:\Windows\System32>python
> Python 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from mycalc import convertPath
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "C:\Python32\lib\site-packages\mycalc.py", line 36
> """
> SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
> in position 149-151: truncated \UXXXXXXX
> X escape
Ha ha, well I'm gratified that my prediction of the error you would
receive under Python 3 is correct, but it doesn't really solve your
problem under Python 2.7.
What happens when you import the code using the standard Python2.7
interactive interpreter? I would expect that the truncated \UXXXXXXXX
error will go away. If you're still getting the earlier SyntaxError, I
would open the file in a hex editor and see what's there. I expect that
there's something unexpected and invisible.
I've seen this happen sometimes when copying and pasting from websites.
For reasons that I'm sure make sense to web designers who haven't given
it any thought, some websites put non-breaking spaces into source code,
which plays merry hell with copying and pasting it because it looks to
the naked eye like a normal space but isn't.
I've also seen ctrl-Z end of file characters in source code, another
invisible to the naked eye thing. Or so-called "high-ASCII" bytes. If
you get an error like this:
SyntaxError: Non-ASCII character '\\xe2' in file script.py on line 21,
but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details
that's your problem.
I don't trust errors given by IDEs, because they often play games with
the source code and sometimes give misleading error messages. Whenever
you have mysterious syntax errors you can't explain, bypass the IDE and
go straight to the standard Python interactive interpreter. (Make sure
you use the same version though.)
--
Steven
More information about the Tutor
mailing list