3

I converted my Python script to a Mac.app (via py2app). I try to run it and get the following error:

SyntaxError: Non-UTF-8 code starting with '\xcf' in file 
py2app/dist/myapp.app/Contents/MacOS/myapp on line 1, but no encoding declared; see 
http://python.org/dev/peps/pep-0263/ for details

I visited the PEP website and added the following to the first two lines of my script:

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

I have also put my code into various online tools (such as this one) to check whether there are any non-UTF-8 characters but I'm not getting any issues.

I did copy some text from an Excel file however there were no special symbols that I was aware of.

The script is approx 800 lines so is there a way of identifying the problem that doesn't involve manually scanning the script line-by-line?

EDIT

Not exactly a fix, but converting my script into an executable instead of a .app has fixed the issue and it now runs correctly.

DDiran
  • 533
  • 1
  • 6
  • 23
  • Maybe see if you have any of these characters: https://tripleee.github.io/8bit/#cf – tripleee Sep 29 '19 at 11:16
  • If you copy/pasted into an on-line UTF-8 checker then your editor talking to your browser by way of copy/paste buffer probably converted the text on the way. – tripleee Sep 29 '19 at 11:17
  • Try `iconv -t utf-16le file >/dev/null` and examine the error output. – tripleee Sep 29 '19 at 11:19
  • @tripleee no error output with that command... every check I've run so far has told me it's all UTF-8 – DDiran Sep 29 '19 at 11:21
  • Can you add a hex dump of `py2app/dist/myapp.app/Contents/MacOS/myapp` please? (MacOS has a command `xxd`; maybe prune the output if it's excessive.) – tripleee Sep 29 '19 at 11:27
  • Oh, `/usr/bin/python` certainly points to Python 2; is the script you tried to convert really a Python 2 script? – tripleee Sep 29 '19 at 11:37
  • @tripleee that's a very good point... although it technically shouldn't matter as it's a mac.app now? here's the hex dump https://pastebin.com/2BSkMRdF – DDiran Sep 29 '19 at 11:41
  • That's not a proper hex dump, but it clearly shows a binary of some sort. Do you get a tracback? – tripleee Sep 29 '19 at 11:44
  • @tripleee no the only error I get is the one posted above. I have also changed `/usr/bin/python` to point to python3 but it still doesn't work – DDiran Sep 29 '19 at 11:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200121/discussion-between-ddiran-and-tripleee). – DDiran Sep 29 '19 at 11:50
  • You reallyereally mustn't mess with the contents of `/usr/bin`. I have removed the duplicate pointing to https://stackoverflow.com/questions/10589620/syntaxerror-non-ascii-character-xa3-in-file-when-function-returns-%c2%a3 – tripleee Sep 29 '19 at 11:50
  • This really needs a [mcve]. Your question is off-topic without it. – Ulrich Eckhardt Sep 29 '19 at 14:57

1 Answers1

2

Python 3 uses UTF-8 as default encoding. This simplify the codes you get from Internet (and other packages). \xcf in UTF-8 is valid only if the byte before has predefined values, which it is not the case: Non-UTF8 code starting mean this, it is not a valid start (first byte) of UTF8 codepoint encoding.

As you see in the comment, you may convert the file into UTF-8, many times you can ignore the initial encoding (often such errors are from comments, e.g. author name). you may convert it, e.g. on options in Saving As on your original editor.

As an alternate way, you can specify the encoding on the first few lines of your code, see PEP-263 on how to do it. Note: Python has hardcoded byte strings to check [because it has not idea of encoding], so try to copy exactly the string as in such document. I think such line # -*- coding: latin-1 -*- should be ok, but this could misinterpret some characters, so test your program. If you do no know the original encoding, the easier way it is to convert original source (because you should in any case check all strings in the source code, and check if you guessed the correct encoding).

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32
  • i added this and its works, i speaking spanish and use vscode editor `#!/usr/bin/python # -*- coding: latin-1 -*-` – Gonen09 Feb 23 '23 at 22:12