如何在使用py2app将python 3.6文件转换为.app后运行.app时发生错误?

时间:2017-12-20 04:43:13

标签: python py2app

我正在使用py2app将我的Python 3.6文件转换为.app,但是,每次我转换它时,在尝试运行.app文件时,都会收到以下错误消息:

enter image description here

我相信我的setup.py文件设置正确:

from setuptools import setup
setup(
    app=["algorithm.py"],
setup_requires=["py2app"],
)

这是我的主要代码(我试图将其转换为.app):

#CENTRALCOAST: 2250-2420
#CENTRALCOAST2: 2250-2267
#NORTHERNBEACHES: 2084-2108
CentralCoast = []
NorthernBeaches = []
OOR = []
Invalid = []
import math
def numLen(num):
  return len(str(abs(num)))

with open('postcodes.txt') as input_file:
    long_list = [line.strip() for line in input_file]
    for i in range(len(long_list)):
        long_list[i] = int(long_list[i])
for each in long_list:
    if 2084 <= each <= 2108: #NorthernBeaches
        NorthernBeaches.extend([each])
for each in long_list:
    if 2250 <= each <= 2267: #CentralCoast
        CentralCoast.extend([each])
for each in long_list:
    if not 2250 <= each <= 2267:
        OOR.extend([each])
#for each in long_list:
#    if numLen(each) != 4:
#        Invalid.extend([each])

Total = len(CentralCoast) + len(OOR) + len(NorthernBeaches) + len(Invalid)

print("Central Coast:", len(CentralCoast), "------", round(len(CentralCoast)/Total,2), "%")
print("")
print("Northern Beaches:", len(NorthernBeaches), "------", round(len(NorthernBeaches)/Total,4), "%")
print("")
print("Out of Range:", len(OOR), "------", round(len(OOR)/Total,2), "%")
print("")
#i = 0
#for i in OOR:
#  print(i)
#  i = i + 1
print("Invalid Entry:", len(Invalid), "------", round(len(Invalid)/Total,4), "%")
print("")
print("")
print("Total:", Total)
exit = input("")

我认为错误可能与主程序使用外部文本文档(postcodes.txt)有关。这是问题吗?

1 个答案:

答案 0 :(得分:1)

我能够为我的Tkinter脚本找到一个可能有助于解决问题的解决方案:

Mac .app-files在.app文件中隐藏自己的文件系统,即MyApplication.app/Contents/MacOS/MyApplication。当您尝试使用“open()”打开文件时,就像使用postcodes.txt一样,并且就像我在脚本中所做的那样,应用程序正在查找内部文件系统(“MyApplication.app/Contents/MacOS/”) ,这似乎被MacOSX禁止,因为即使使用“try / except”,我也收到了错误消息。

解决方案是将整个路径添加到文件中,即

test = open("/Users/yourusername/Documents/test.txt", "r")

如果您想在其他Mac上使用该应用,并且您想将该文件放在桌面或文档文件夹中,请确保您没有输入用户名:

import os
file = open(os.path.expanduser("~/Documents/test.txt"), "r")