如何在pyinstaller中为textblob添加一个钩子

How to add a hook for textblob in pyinstaller

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

它return在控制台上执行时的正确值 但是 return 0 当用 .exe

执行时
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

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

from textblob import TextBlob

start_dmo():
    ....

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

如果你使用 numpy 或 pandas 你可能需要添加选项 cz 它可能不会导入 numpy 来形成你的 exe 所以解决这个问题按照下面 link: Creating cx_Freeze exe with Numpy for Python

祝你好运!

Pyinstaller 未在包中包含 en-sentiment.xml,因此情绪分析器缺少依赖项和 returns0。在这种情况下,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 上查看我的 post。

嘿,将站点包中的 textblob 复制到您的工作目录 和下面的 .spec

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

它会起作用