【问题标题】:How to add a hook for textblob in pyinstaller如何在 pyinstaller 中为 textblob 添加挂钩
【发布时间】:2017-12-27 07:09:40
【问题描述】:

我正在尝试使用 pyinstaller 创建一个 .exe 文件并执行它 它没有从中获取任何结果 b = TextBlob(ar) score = b.sentiment.polarity

在控制台执行时返回正确的值 但使用 .exe 执行时返回 0

def start_dmo():
 print ("Importing all the required packages...")
 from textblob import TextBlob
 input ("press enter to continue")
 print("All Necessary Packages imported")
 input("press enter to continue")
 ar="I cant be more happy with this"
 print(ar)
 b = TextBlob (ar)
 score = b.sentiment.polarity
 print (b.sentiment.polarity)
 input("press enter to continue")
 score = round (score, 2)
 if score > 0.0:
    senti = "Positive"
elif score < 0.0:
    senti = "Negative"
else:
    senti = "Neutral"
 print("Score"+str(score)+"sentiment"+senti)
 input("press enter to continue")

start_dmo()

this is the output when the above code is executed on console

this is the output when the above code is executed on .exe of the same code which is created using pyinstaller

【问题讨论】:

  • @user772401 你能帮帮我吗?谢谢

标签: hook pyinstaller textblob


【解决方案1】:

Pyinstaller 未在包中包含 en-sentiment.xml,因此情绪分析器缺少依赖项并返回 0。在这种情况下,Textblob 不会产生错误。

pyinstaller 要求您手动指定 myscript.spec 中的任何数据文件。但是,正如您所发现的,cx_Freeze 似乎遵循 setup.py 配置,它指定了要包含的数据文件:

package_data={
  "textblob.en": ["*.txt", "*.xml"]
}

要解决此问题,请修改 pyinstaller myscript.spec 文件以复制 textblob/en/en-sentiment.xml,或按照讨论切换到 cx_Freeze

也可以查看我在 Github 上的帖子。

【讨论】:

  • 这为我解决了。就我而言,问题是间歇性的,有些文本会返回分数,有些则不会。在明确包含它现在可以使用的文件之后,不知道为什么有些在包含它们之前有效。
【解决方案2】:

尝试在 start_dmo 函数之前导入 textblob,以便 pyinstaller 将其视为依赖项。

from textblob import TextBlob

start_dmo():
    ....

【讨论】:

    【解决方案3】:

    问题已解决! 简单的解决方案 将 pyinstaller 更改为 cx_Freeze 仅供参考 cx_Freeze 与 python 3.6 完美配合 要了解如何使用 cx_freeze 创建 .exe,请点击以下链接: https://pythonprogramming.net/converting-python-scripts-exe-executables/

    如果您使用 numpy 或 pandas,您可能需要添加选项 cz,它可能不会导入 numpy 来形成您的 exe,因此请按照以下链接解决该问题: Creating cx_Freeze exe with Numpy for Python

    祝你好运!

    【讨论】:

      【解决方案4】:

      嘿,将 textblob 从站点包复制到您的工作目录 并在 .spec 中运行

        a = Analysis(.....
               datas=[( 'textblob/en/*.txt', 'textblob/en' ),
           ( 'textblob/en/*.xml', 'textblob/en' )],
           ....)
      

      会有用的

      【讨论】:

        猜你喜欢
        • 2019-06-16
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-04
        • 2018-05-01
        相关资源
        最近更新 更多