my_app.app(终止)和 my_app.app/Contents/MacOS/my_app(完美运行)的不同行为

Different behaviour of my_app.app (terminating) and my_app.app/Contents/MacOS/my_app (working perfectly)

我有一个函数 test(),如下所示,下载包含表情符号和其他 unicode 特殊字符的 txt-file。下载的内容保存到 .txt 文件,然后每行添加行号,保存到 .csv 文件。

当我构建 test.app(使用 py2app)和 运行 它时,它确实保存了 .txt 文件(显示与原始文件相同的内容)但是当保存 .csv 时,它停止在第一个特殊字符(错误消息 test Error: Open console / Terminate; system.log 显示 'Service exited with abnormal code: 255')。

另一方面,当运行ning测试时。app/Contents/MacOS/test,它工作正常并且将整个内容保存到.csv没有任何问题。

我已经用纯 Python 版本以及 cythonized 和代码签名对其进行了测试。在所有情况下,.app 不工作而 .app/Contents/MacOS/test 正常工作。

我想知道它为什么不同(我 3 个月前才开始使用 Mac 并且绝对不知道它的一切)以及我应该改变什么才能获得我的 test.app 正常工作。

def test():
    url = 'https://unicode.org/Public/emoji/1.0/emoji-data.txt'
    log_filename = 'emoji-data.log'
    globals.log_file = open(log_filename, 'w', buffering=1)

    output_txt = 'test.txt'
    output_csv = 'test.csv'
    keys = ('row_nr', 'row')
    with open(output_csv, 'w') as outfile:
        writer = csv.writer(outfile, delimiter=',')
        writer.writerow(keys)

    logging.info(f'going to download...')
    with requests.get(url, timeout=5) as resp:
        with open(str(output_txt), 'wb') as f:
            for chunk in resp.iter_content(chunk_size=8192):
                f.write(chunk)

    encoding = resp.encoding
    if encoding is None:
        logging.info('Unknown encoding!')
        encoding = 'utf-8'
    logging.info(f'encoding = {encoding}')
    resp = resp.content.decode(encoding)
    logging.info('decoded resp')

    for i, row in enumerate(resp.split('\n')):
        logging.info(f'row {i}: {row}')

        with open(output_csv, 'a') as outfile:
            writer = csv.writer(outfile, delimiter=',')
            writer.writerow((i, row))
            
    logging.info('... done')

    # closing:
    if log_file:
        log_file.close()
    return

test()

表情符号-data.log:

going to download...
encoding = utf-8
decoded resp
row 0: # Emoji Data for UTR #51
row 1: #
row 2: # File:    emoji-data.txt
row 3: # Version: 1.0
row 4: # Date:    2015-08-04
row 5: #
row 6: # Copyright (c) 2015 Unicode, Inc.
row 7: # For terms of use, see http://www.unicode.org/terms_of_use.html
row 8: # For documentation and usage, see http://www.unicode.org/reports/tr51/
row 9: #
row 10: # Format: Code ; Default_Emoji_Style ; Emoji_Level ; Emoji_Modifier_Status ; Emoji_Sources # Comment
row 11: #

test.csv:

row_nr,row
0,# Emoji Data for UTR #51
1,#
2,# File:    emoji-data.txt
3,# Version: 1.0
4,# Date:    2015-08-04
5,#
6,"# Copyright (c) 2015 Unicode, Inc."
7,"# For terms of use, see http://www.unicode.org/terms_of_use.html"
8,"# For documentation and usage, see http://www.unicode.org/reports/tr51/"
9,#
10,# Format: Code ; Default_Emoji_Style ; Emoji_Level ; Emoji_Modifier_Status ; Emoji_Sources # Comment
11,#

app 运行s 与您直接在命令行 运行 时使用的语言环境不同。

尝试输入你的.py :

import locale
locale.setlocale(locale.LC_ALL, ('C', 'UTF-8'))

有关 C 语言环境的说明,请参阅 https://docs.oracle.com/cd/E23824_01/html/E26033/glmbx.html