[Python] pyautogui problem integration when generating .exe with pyinstaller

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a problem with a program that uses Selenium to manipulate the web and integrates a class we created that encapsulates the use of pyautogui to simulate key pressing.

    When I run the program in VSC, there’s no error, but when I generate the .exe using pyinstaller and then run the program, the part that uses pyautogui just doesn’t work. I also tried to use other libraries such as keyboard or pywinauto, but it’s the same story.

    The piece of code that fails looks like this:

    def download_salesforce_reports():
    '''
    Downloads the reports from Salesforce based on the list of codes of excel file, and
    '''

    global keyboard, screen_controller

    file_path = global_config['excel_file']
    current_sheet = 'Sheet1'
    wbc = WebController("Google")
    if "chromedriver.exe" not in os.listdir(os.getcwd()):
    wbc.downloadWebDriver()
    wbc.startWindow(url="https://example-url.com")

    # Obtains the info of the codes to pass as input in Salesforce
    list_numero_cuentas = get_codes_list(file_path, current_sheet, 'Funda empaque')
    list_numero_cuentas_pdfs = []
    for num_cuenta in list_numero_cuentas:
    # Update current_report data to show in UI
    global_config['status']['current_report'] = num_cuenta

    # Select input, write code and press Enter
    screen_controller.see_image_and_click(f'{global_config["path_dir_img_salesforce"]}\\01.png')
    time.sleep(1)
    keyboard.press_hot_key('ctrl', 'a')
    keyboard.press_key('delete')

    time.sleep(1.5)
    keyboard.write_text(num_cuenta)
    time.sleep(1.5)
    keyboard.press_key('down')


    As you can see, I created classes that encapsulate pyautogui (keyboard object) and Selenium (WebController) methods, and while the Selenium logic works correctly, the part of using the keyboard just doesn’t work when I run my created .exe file. No errors are indicated; instead, the console indicates the execution of that code part is correct, but the problem is that it just doesn’t work.

    The command that I use to create the .exe is this one:

    pyinstaller --onefile --hidden-import python --add-data "web;web" --add-data "python/config.py;python" --add-data "python/;python" --add-data "python/download_reports.py;python" --hidden-import comtypes.stream python/main.py


    I would be very thankful if someone can guide me or tell me if they see something wrong here.

    Continue reading...

Compartilhe esta Página