You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python文章摘要代码打包为App后功能失效,如何解决?

问题:Python文章摘要脚本打包为App后无法生成摘要

我用Python编写了一个简易文章摘要脚本,原代码运行正常,可弹出界面供用户输入URL,点击按钮即可生成摘要。但打包为App后,界面可正常弹出、能输入URL,却无法生成摘要。

脚本代码:

import tkinter as tk
import nltk
from newspaper import Article
# nltk.download('punkt')  # 后续会修改这一行,先注释


def summarize():
    url = urltext.get('1.0', "end").strip()

    article = Article(url)
    article.download()
    article.parse()
    article.nlp()

    # 允许编辑文本框
    title.config(state='normal')
    authors.config(state='normal')
    publicationdate.config(state='normal')
    summary.config(state='normal')
    
    title.delete('1.0', "end") # 清空原有内容
    title.insert('1.0', article.title)

    authors.delete('1.0', "end") 
    authors.insert('1.0', article.authors)

    publicationdate.delete('1.0', "end") 
    publicationdate.insert('1.0', article.publish_date)

    summary.delete('1.0', "end") 
    summary.insert('1.0', article.summary)

    # 禁止用户编辑文本框
    title.config(state='disabled')
    authors.config(state='disabled')
    publicationdate.config(state='disabled')
    summary.config(state='disabled')


# 界面部分
root = tk.Tk()
root.title("新闻摘要工具")
root.geometry('1200x600') # 窗口尺寸

tlabel = tk.Label(root, text="标题")
tlabel.pack()
title = tk.Text(root, height=1, width=140)
title.config(state='disabled', bg='#dddddd')
title.pack()

alabel = tk.Label(root, text="作者")
alabel.pack()
authors = tk.Text(root, height=1, width=140)
authors.config(state='disabled', bg='#dddddd')
authors.pack()

pdlabel = tk.Label(root, text="发布日期")
pdlabel.pack()
publicationdate = tk.Text(root, height=1, width=140)
publicationdate.config(state='disabled', bg='#dddddd')
publicationdate.pack()

slabel = tk.Label(root, text="摘要")
slabel.pack()
summary = tk.Text(root, height=20, width=140)
summary.config(state='disabled', bg='#dddddd')
summary.pack()

urllabel = tk.Label(root, text="URL地址")
urllabel.pack()
urltext = tk.Text(root, height=1, width=140)
urltext.config(bg='#dddddd')
urltext.pack()

button = tk.Button(root, text="生成摘要", command=summarize)
button.pack()

root.mainloop()

打包步骤:

  • 进入代码所在文件目录
  • 在路径栏输入“cmd”打开命令提示符
  • 执行命令pip install pyinstaller
  • 执行命令pyinstaller --onefile -w FILENAME.py

打包后生成两个文件夹:

  • dist:存放可执行文件
  • build:可删除

解决方案:

1. 处理NLTK的Punkt资源缺失问题

打包后的程序无法自动下载NLTK的Punkt分词资源,需要手动引入:

  • 先运行原脚本,等待nltk.download('punkt')执行完成,通过print(nltk.data.path)查看NLTK资源路径(通常在用户目录的AppData\Roaming\nltk_data\tokenizers下)
  • 复制tokenizers目录下的punkt文件夹到脚本所在目录
  • 修改脚本,添加资源路径指定:
import os
import nltk
nltk.data.path.append(os.path.join(os.path.dirname(__file__), 'punkt'))

同时注释掉原代码中的nltk.download('punkt')

2. 确保Newspaper库依赖被完整打包

使用PyInstaller的--collect-all参数打包Newspaper的所有依赖:

pyinstaller --onefile -w --collect-all newspaper FILENAME.py

3. 调试时查看报错信息

暂时去掉-w参数打包,运行控制台版本,查看具体报错内容,定位问题:

pyinstaller --onefile FILENAME.py

运行dist目录下的可执行文件,控制台会输出错误信息(比如模块缺失、路径错误等),根据提示修复后再重新打包。


内容的提问来源于stack exchange,提问作者L.O.C.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:50:24