使用 PyInstaller 制作的 .app 会立即关闭吗? [英] .app made using PyInstaller closes straight away?

查看:43
本文介绍了使用 PyInstaller 制作的 .app 会立即关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 PyInstaller 将我的 python 程序转换为 .app.实际代码通过 IDLE 运行良好,但每次我尝试运行新转换的 .app 时,它都会立即关闭.下面是我的 .spec 文件和我的 .py 文件.我已经编辑了 .spec 文件,添加了我在 .py 文件中导入的文本文件.

I have attempted to convert my python program into a .app using PyInstaller. The actual code runs fine through IDLE, but everytime I try and run the newly converted .app, it closes straight away. Below is my .spec file and my .py file. I have edited the .spec file, adding in the text file I import in my .py file.

Python 文件:

#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("")

规格文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['algorithmPOSTCODE.py'],
             pathex=['/Users/CooperTimewell'],
             binaries=[],
             datas=[('postcodes.txt', '.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='algorithmPOSTCODE',
          debug=False,
          strip=False,
          upx=True,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='algorithmPOSTCODE')
app = BUNDLE(coll,
             name='algorithmPOSTCODE.app',
             icon=None,
             bundle_identifier=None)

如何阻止它立即关闭?谢谢.

How do I stop it from closing straight away? Thankyou.

推荐答案

我遇到了和你一样的问题,尽管我知道我的代码不应该立即关闭,因为它在等待 user_input.当我使用 python my_script.py 从终端运行我的代码时,程序会运行良好.

I had the same problem as you even though I knew my code wasn't supposed to close immediately since it waited for user_input. When I ran my code from the terminal using python my_script.py the program would run fine.

这是我修复它的方法:

我使用以下方法重新安装了 pyinstaller:

I reinstalled pyinstaller using:

pip install pyinstaller

我认为这是我的主要事情,因为我第一次安装它时,我相信我的防病毒软件阻止了某些组件的正确安装,当我重新安装它时,它可能修补了漏洞.

I think that this was my main thing because the first time I installed it, I believe my antivirus prevented some of the components from installing correctly and when I reinstalled it, it probably patched in the holes.

我还尝试了不同的命令行命令.我明确声明最终的 .exe 使用 -c 标志打开控制台并保持打开状态.注意:在 Windows 上,如果第一个脚本是.pyw"文件,则此选项无效.它看起来像这样:

I also tried a different command-line command. I explicitly stated for the final .exe to open a console and keep it open using the -c flag. Note: On Windows, this option will have no effect if the first script is a ‘.pyw’ file. It looked like this:

pyinstaller -c -F -i cm_icon.ico console_monopoly.py 

-F 标志是将所有内容都捆绑到一个 .exe 中,而不是在 dist/文件夹中围绕我的 .exe 放置大量文件.

the -F flag was to bundle everything into one .exe instead of having a lot of files surrounding my .exe in the dist/ folder.

-i 标志用于向我的程序添加图标.

the -i flag is for adding an icon to my program.

希望这会有所帮助!

这篇关于使用 PyInstaller 制作的 .app 会立即关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆