【发布时间】: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