【问题标题】:Issue with converting .py (Python) file to executable (Python 3.x)将 .py (Python) 文件转换为可执行文件 (Python 3.x) 的问题
【发布时间】:2020-04-26 15:27:29
【问题描述】:

我使用 python 3 编写了一个桌面应用程序,它是一个 API 数据收集应用程序。当我创建可执行文件 (.exe) 时,它可以在我的计算机上完美运行,但是当我在其他计算机上打开它时,我得到了

执行脚本失败消息。

代码我只导入以下内容。

from tkinter import*
import requests
import json 

然后我添加API如下。

try:
#Connect the API to the .py code
    api_data=requests.get("https://www.hpb.health.gov.lk/api/get-current-statistical")
    api_json=json.loads(api_data.content)
    #Connect with variables
    lu_date= api_json['data']['update_date_time']
    lu_cases= api_json['data']['local_new_cases']
    lu_total= api_json['data']['local_total_cases']
    lu_hospital= api_json['data']['local_total_number_of_individuals_in_hospitals']
    lu_deaths= api_json['data']['local_deaths']
    lu_recover= api_json['data']['local_recovered']
    lu_rate1=int((lu_deaths/lu_recover)*100)
#Added some labels here
#Button
    ext_btn=Button(root, text="Exit", command=root.destroy,bg="red",fg="white",font="Helvatica 12 bold",padx=25)
    ext_btn.grid(row=8,column=0,columnspan=2)

    out = out.decode(encoding)

except Exception as e:
    api_json="Error"
root.mainloop()

我已经尝试了以下方法来执行它,但它不起作用。 (通过使用命令行以及给定的 GUI 软件)

pyinstaller -F codename.py

【问题讨论】:

  • 您正在接受所有错误。您可能需要考虑检查可能引发的异常。
  • 感谢您的宝贵意见,我将更多地关注异常处理并改进代码。但是这个可执行文件在我以前编码的计算机上运行良好,但在其他计算机上运行良好
  • “但是这个可执行文件在我以前编码的计算机上运行良好,但在其他计算机上却不行” - 对。这就是我要求您检查几乎肯定会出现的错误的方式。错误文本可能会告诉您为什么它不能在另一个系统上运行。解决问题的第一步是了解问题。
  • 非常感谢您的宝贵意见。我是编码新手,所以我可能会犯错误。你能指出我应该在哪里检查更多吗?
  • except 语句中添加打印语句

标签: json api python-3.7 executable


【解决方案1】:

经过这么多的研究和方向,我能够找到问题所在。谢谢@Bryan Oakley 的指导。 问题 这是一个 Tkinter 问题,通过删除图标导入(窗口左上角图标),我可以解决这个问题。

#root.iconbitmap('icon.ico')

另外,我添加了一个可靠的异常处理语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2012-08-08
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2015-01-05
    相关资源
    最近更新 更多