Search code examples
pythonpy2app

How to stop error occuring when running .app after converting python 3.6 file to .app using py2app?


I am using py2app to convert my Python 3.6 file to .app, however, every time I convert it, when trying to run the .app file, I get this error message:

enter image description here

I believe my setup.py file is set up correctly:

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

This is my main code (the one I am trying to convert to .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("")

I was thinking the error might have something to do with the fact the main program uses an external text document (postcodes.txt). Is this the problem?


Solution

  • I was able to find a solution for my Tkinter script that might help to solve your problem as well:

    Mac .app-files hide their own file system inside the .app-file, i.e. MyApplication.app/Contents/MacOS/MyApplication. When you try to open a file with "open()", like you did with postcodes.txt and like I did in my script, the app is looking in that internal file system ("MyApplication.app/Contents/MacOS/"), which seems to be forbidden by MacOSX, because even with "try/except" I was getting that error message.

    The solution was adding the whole path to the file, i.e.

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

    If you want to use the app on other Macs and you want to place the file on the desktop or the documents folder, make sure you don't type out the username:

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