Python encoding problem

Hi,

got some trouble with a simple Python script. I’m sure that there are a few Python experts here. When I execute my script with Xojo then everything is okay. After building with Py2App I get an error message when executing: “Syntax error: non utf-8 starting with ‘\0xca’ in file xxx on line 1, but no encoding declared.”

The beginning of the file is

[code]#!/usr/bin/env python3

-- coding: utf-8 --

from future import unicode_literals
import base64
import tempfile
import os
import sys
import socket
import imaplib
imaplib._MAXLINE = 1000000
imaplib.Debug = 0[/code]

Does anyone have an idea what Python 3.4.x wants to tell me? I’ve had a look at the source code file with 0xED and there is no character ca in the file. Let alone in the first line.

Well, as for Python 3.x the default encoding should be UTF-8, try to remove the encoding declaration line and report results.

Also you can try peep ( http://peacockmedia.software/mac/peep/ ) to determine encoding and convert it to utf-8 if necessary. http://peacockmedia.software/mac/peep/

[quote=203219:@Beatrix Willius]Hi,

got some trouble with a simple Python script. I’m sure that there are a few Python experts here. When I execute my script with Xojo then everything is okay. After building with Py2App I get an error message when executing: “Syntax error: non utf-8 starting with ‘\0xca’ in file xxx on line 1, but no encoding declared.”

The beginning of the file is

[code]#!/usr/bin/env python3

-- coding: utf-8 --

from future import unicode_literals
import base64
import tempfile
import os
import sys
import socket
import imaplib
imaplib._MAXLINE = 1000000
imaplib.Debug = 0[/code]

Does anyone have an idea what Python 3.4.x wants to tell me? I’ve had a look at the source code file with 0xED and there is no character ca in the file. Let alone in the first line.[/quote]

Dump the file in a hex editor and you’ll probably find the offending byte. This often happens to me when I paste in text that turns out to contain smart quotes, unicode hyphens, and the link.

@Charles and Rick: thanks for the tips. It’s just so weird that the script works and the app not.

Did some testing:

  • Peep: great app, didn’t make a difference.
  • Reverted back to a version that should work: no difference.

It must be something on my machine, because an older version of the Python app crashes (fatal error something) on my main dev machine. The app works just fine on my laptop. For now I’ve cheated and copied the py file to the app that worked.

Is my Python installation hosed somehow?

I am not an expert on Py2App. But what happen if you run your Py2App with --emulate-shell-environment ?