milu1123 发表于 2023-11-14 14:52

python 打包exe

各位老大,以下代码我使用pyinstaller命令打包成 exe 为什么要闪退呢?
pycharm上运行正常,那个老大能指点指点嘛?


import json
import random
import requests
import datetime
import keyboard

head = [
    {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2919.83 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2866.71 Safari/537.36'},
    {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2820.59 Safari/537.36'}]
def main(url):
    a = datetime.datetime.now()
    today = datetime.date.today()
    day_of_week = today.weekday()
    c = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    time1 = a.strftime(f"%Y年%m月%d日 {c} %H:%M:%S")
    b = datetime.datetime(2024, 2, 9)
    d = datetime.datetime(2024, 1, 1)
    count_down = (b - a).days
    count_down1 = (d - a).days
    resp = requests.get(url, headers=head)
    r = json.loads(resp.text)
    content = r["content"] + "\n" + r["note"]
    print("今天是", time1, "\n", content, "\n", f"距离除夕还有{count_down}天, 元旦还有{count_down1}天")

# info = "今天是"+time+"\n"+content+f"距离除夕还有{count_down}天"
# data = {"method": "sendText", "wxid": "ArayofSunshine1992", "msg": info}
# url = f'http://127.0.0.1:8203/api?json={json.dumps(data)}&key=44468FAE112ACFB7F45643CC5CBC55A5D8DCD006'
# data = {"method": "sendText", "wxid": "Axiaoxiao199996", "msg": info}
# url1 = f'http://127.0.0.1:8203/api?json={json.dumps(data)}&key=44468FAE112ACFB7F45643CC5CBC55A5D8DCD006'
# requests.get(url)
# requests.get(url1)

    while True:
      if keyboard.is_pressed('esc'):
            print("exiting ,program...")
            break


if __name__ == '__main__':
    url = "http://open.iciba.com/dsapi/"
    main(url)

hanloth 发表于 2023-11-14 14:58

pyinstaller封装的好像不能有第47行的判断

milu1123 发表于 2023-11-14 15:02

hanloth 发表于 2023-11-14 14:58
pyinstaller封装的好像不能有第47行的判断

那我该如何封装呢??

请教大佬。。。

fortytwo 发表于 2023-11-14 15:03

新打开个cmd窗口调用你这个exe,他会出报错信息,根据信息查看原因
(常规双击打开如果出错会关闭掉原来的cmd窗口,导致看不到报错信息)

milu1123 发表于 2023-11-14 15:07

fortytwo 发表于 2023-11-14 15:03
新打开个cmd窗口调用你这个exe,他会出报错信息,根据信息查看原因
(常规双击打开如果出错会关闭掉原来的 ...

Traceback (most recent call last):
File "Ti.py", line 49, in <module>
File "Ti.py", line 23, in main
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error
Failed to execute script 'Ti' due to unhandled exception!


以上是报错信息,,,第一次学习打包,,不会知道如何修改错误

18834161486 发表于 2023-11-14 15:14

hanloth 发表于 2023-11-14 14:58
pyinstaller封装的好像不能有第47行的判断

这个应该不影响,因为我以前也写了而且打包成功了

1022556987 发表于 2023-11-14 15:15

没有任何问题 打包命令 pyinstaller -F xxx.py 不要加w

zfb 发表于 2023-11-14 15:20

这报错说明跟pyinstaller没关系,就是当前的运行环境不支持中文。
Traceback (most recent call last):
File "Ti.py", line 49, in <module>
File "Ti.py", line 23, in main
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error
Failed to execute script 'Ti' due to unhandled exception!

就是说地23行的"年"字是不支持的编码类型

milu1123 发表于 2023-11-14 15:20

1022556987 发表于 2023-11-14 15:15
没有任何问题 打包命令 pyinstaller -F xxx.py 不要加w

一样闪退,,,,,,我运行的exe程序是dist目录下面的。。、。

平淡最真 发表于 2023-11-14 15:21

https://blog.csdn.net/lanxingbudui/article/details/124018316
页: [1] 2 3 4
查看完整版本: python 打包exe